{"id":13745389,"url":"https://github.com/danielwanja/activeresource","last_synced_at":"2025-05-09T05:32:01.870Z","repository":{"id":66396485,"uuid":"2212398","full_name":"danielwanja/activeresource","owner":"danielwanja","description":"A Flex/ActionScript Framework to integrate with Ruby on Rails. Provides Restful access to Rails including nested attributes.","archived":false,"fork":false,"pushed_at":"2012-01-30T20:08:38.000Z","size":24552,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-08-04T05:06:14.853Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://onrails.org","language":"ActionScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/danielwanja.png","metadata":{"files":{"readme":"README.markdown","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-08-15T21:30:20.000Z","updated_at":"2014-01-26T00:50:16.000Z","dependencies_parsed_at":"2023-02-20T03:31:02.838Z","dependency_job_id":null,"html_url":"https://github.com/danielwanja/activeresource","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielwanja%2Factiveresource","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielwanja%2Factiveresource/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielwanja%2Factiveresource/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielwanja%2Factiveresource/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danielwanja","download_url":"https://codeload.github.com/danielwanja/activeresource/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224819951,"owners_count":17375370,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T05:01:28.912Z","updated_at":"2024-11-15T17:31:50.659Z","avatar_url":"https://github.com/danielwanja.png","language":"ActionScript","readme":"# activeresource - An ActionScript Framework to integrate Flex with Ruby on Rails\n\nA Flex/ActionScript Framework to integrate with Ruby on Rails. Provides Restful access to Rails including nested attributes.\n\n## Flex App\n\nCopy the flex/bin/activeresource.swc to the lib folder of your application.\n\n## Declaring the Resources\n\nFirst create a dynamic class that maps to a resource:\n\n```javascript\n\n    dynamic public class Parent extends ActiveResource\n    {\n    \tpublic function Parent(attributes:Object=null)\n    \t{\n    \t\tsuper(attributes);\n    \t}\n\t\n    \t/* static block */\t\t\n    \tresource(\"parents\", Parent); \t\t\n    }\n\n\tdynamic public class Child extends ActiveResource\n\t{\n\t\tpublic function Child(attributes:Object=null)\n\t\t{\n\t\t\tsuper(attributes);\n\t\t}\n\t\t\n\t\t/* static block */\t\t\n\t\tresource(\"children\", Child); \t\t\n\t}\n\t\n```\n\nNote the static _resource_ method is called when defining the class. This links the resource class to a the Rails resource name.\nAlso note that by default ActiveResource.baseUrl points to http://localhost:3000. This works fine during development but you need to either point the resources to your server or use a relative path when your Flex application is served from the public folder of your Rails application. I.e. ActiveResource.baseUrl = \"/\";\n\n## Usage\n\n### Flex\n\nThe Flex ActiveResource class allows to access a Rails resource and to perform a Index, Show, Create, Update and Delete. \n\nFind All:\n\n```javascript\n    var call:AsyncToken = ActiveResource.findAll(Parent)\n\tcall.addResponder(new AsyncResponder(resultHandler, faultHandler));\n```\n\nFind one:\n\n```javascript\n    var call:AsyncToken = ActiveResource.find(Parent, 1)\n\tcall.addResponder(new AsyncResponder(resultHandler, faultHandler));\n```\n\nCreate:\n\n```javascript\n    var parent:Parent = new Parent();\n    parent.name = \"Daniel\";\n    parent.favorite_food = \"Cheese\";\n    var call:AsyncToken = parent.save();\n\tcall.addResponder(new AsyncResponder(resultHandler, faultHandler));\n```\n\nUpdate:\n\n```javascript\n    parent.favorite_food = \"Chocolate\";\n    var call:AsyncToken = parent.save();\n\tcall.addResponder(new AsyncResponder(resultHandler, faultHandler));\n```\n\t\t\nNote I will add a simplified syntax support for the create and update where you can just issue a _parent.save()_\n\nDelete:\n\n```javascript\n    var call:AsyncToken = parent.destroy();\n\tcall.addResponder(new AsyncResponder(resultHandler, faultHandler));\n```\n\n### Rails\n\nThese are the Rails routes involved:\n\n```ruby\n        parents GET    /parents(.:format)                      {:action=\u003e\"index\", :controller=\u003e\"parents\"}\n                POST   /parents(.:format)                      {:action=\u003e\"create\", :controller=\u003e\"parents\"}\n         parent GET    /parents/:id(.:format)                  {:action=\u003e\"show\", :controller=\u003e\"parents\"}\n                PUT    /parents/:id(.:format)                  {:action=\u003e\"update\", :controller=\u003e\"parents\"}\n                DELETE /parents/:id(.:format)                  {:action=\u003e\"destroy\", :controller=\u003e\"parents\"}\n```\n\nYou can generate a default scaffolded controller for the _parent_ resource and use to Flex ActiveResource framework to consume that resource. The Flex ActiveResource class uses the JSON format. One ActiveResource provides access to the different Rails routes (urls) to perform create, update and deletes.\n\n```\n    rails generate scaffold parent name:string birthday:date single:boolean\n```\n\nThe following is an extract of the ParentController and shows the default generated _index_ and _create_ methods.\n\n```ruby\n    # GET /parents.json\n    def index\n      @parents = Parent.all\n\n      respond_to do |format|\n        format.html # index.html.erb\n        format.json { render json: @parents }\n      end\n    end\n    \n    # POST /parents.json\n    def create\n      @parent = Parent.new(params[:parent])\n\n      respond_to do |format|\n        if @parent.save\n          format.html { redirect_to @parent, notice: 'Parent was successfully created.' }\n          format.json { render json: @parent, status: :created, location: @parent }\n        else\n          format.html { render action: \"new\" }\n          format.json { render json: @parent.errors, status: :unprocessable_entity }\n        end\n      end\n    end    \n```\t\t\n\n\n### Nested Attributes:\n\nRails support nested attributes where complex data structures can be sent in one request to the server. For example in the following case a Parent model has many Children. By adding the _accepts_nested_attributes_for_ declaration to the parent active record you allow providing children attributes which can be used to create, update and delete children that are associated with the parent. \n\n\n```ruby\n\tclass Parent \u003c ActiveRecord::Base\n\t  has_many :children\n      accepts_nested_attributes_for :children, :allow_destroy =\u003e true\n\tend\n\t\n\tclass Child \u003c ActiveRecord::Base\n\t  belongs_to :parent\n\tend\n```\n\nThen you can update the parent and the child records in one request:\n\n```javascript\n   parent.children.addItem(new Child({first_name:'Rockie'}));\n   parent.childreen.getItemAt(5)._destroy = true;\n   var call:AsyncToken = parent.save({nestedAttributes:['children']})\n   call.addResponder(new AsyncResponder(resultHandler, faultHandler));\n```\n\nNote that the default generated Rails controller just returns an :ok after a successful update. If you wan't the update record and child records are returned you can modify the update method of the controller as follows:\n\n```ruby\n# PUT /parents/1.json\n    def update\n      @parent = Parent.find(params[:id])\n\n      respond_to do |format|\n        if @parent.update_attributes(params[:parent])\n          format.html { redirect_to @parent, notice: 'Parent was successfully updated.' }\n          format.json { render json: @parent, status: :ok } # was { head :ok }\n        else\n          format.html { render action: \"edit\" }\n          format.json { render json: @parent.errors, status: :unprocessable_entity }\n        end\n      end\n    end\n```\nNote that the statement _head :ok_ was replace by _render json: @parent_\n\nand you can refine the JSON that is returned by the active record to always include the children of the parent:\n\n```ruby\n    class Parent \u003c ActiveRecord::Base\n      has_many    :children\n      accepts_nested_attributes_for :children, :allow_destroy =\u003e true\n\n      def as_json(options={})    \n        super(:include =\u003e :children)\n      end\n    end\n```  \n  \nFIXME: deleting children is a bad example/taste in this case. Maybe an Order and it's OrderItems, or a Department and it's Employees...\n\n### RoadMap\n\n1. extend test coverage \n2. unify base classes with bulk_api_flex?\n3. improve server side validation support \n\n### Credits\n\nThanks to Pinnacol Assurance for providing some of the base code and concepts on which this project is built.\n\n\n### Community\n\nWant to help? Contact Daniel Wanja d@n-so.com\n\nEnjoy!\nDaniel Wanja\n\n","funding_links":[],"categories":["Unsorted"],"sub_categories":["Other API"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielwanja%2Factiveresource","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanielwanja%2Factiveresource","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielwanja%2Factiveresource/lists"}