Created
March 28, 2013 11:00
-
-
Save hasham2/5262362 to your computer and use it in GitHub Desktop.
Acts as API Example: I am using acts_as_api gem here to define this simple API this is part of overall much complex API, The Mongoid based model is part of tree structure for which we have used Mongoid-tree gem, This is why the model class inherits from Node class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CategoriesController < ApplicationController | |
| respond_to :html, :json | |
| before_filter :find, :only => [:edit, :update, :destroy] | |
| def index | |
| parent = load_node_from_params | |
| @nodes = parent.descendants.categories.sort | |
| respond_with @nodes.to_a, :api_template => :basic | |
| end | |
| def new | |
| @category = Category.new | |
| respond_with :@category | |
| end | |
| def create | |
| @category = Category.new(params[:category]) | |
| @category.parent ||= @methodology | |
| @category.save | |
| respond_with @category | |
| end | |
| def update | |
| @category.update_attributes(params[:category]) | |
| respond_with @category | |
| end | |
| def edit | |
| respond_with @category | |
| end | |
| def destroy | |
| @category.destroy | |
| respond_with @category | |
| end | |
| private | |
| def find | |
| @category = Category.where(:id => params[:id]).first | |
| redirect_to(root_path && return) unless @category | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Category < Node | |
| extend ActsAsApi::Base | |
| acts_as_api | |
| api_accessible :detailed do |t| | |
| t.add :name | |
| t.add :uri | |
| t.add :category_uri | |
| t.add :ancestor_names | |
| t.add :question | |
| end | |
| api_accessible :basic do |t| | |
| t.add :name | |
| t.add :uri | |
| t.add :category_uri | |
| t.add :ancestor_names | |
| t.add :question | |
| end | |
| validates :parent, :type=>[Account, Category] | |
| validates :children, :type=>[Category, Template, ValueTable] | |
| delegate :account, :to => :parent | |
| def emission_factor_groups | |
| children.emission_factor_group | |
| end | |
| def uri | |
| generate_path | |
| path.split('/').drop(1).join('/') | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment