Module: BTech::Rest::ClassMethods

Defined in:
lib/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to(parent) ⇒ Object

Allows an object to belong to a parent object, thus creating a nested hierarchy. This behaves in a similar fashion to the belongs_to ActiveRecord macro. Accepts the following parameters:

  • parent - The parent symbol (including namespaces delimited by underscores). Be sure to reflect singular or plural forms on the name. Example: Public::PostsController, Result: :public_posts



7
8
9
10
# File 'lib/class_methods.rb', line 7

def belongs_to parent
  @parent_resource = parent.to_s
  class_eval "class << self; attr_accessor :parent_resource end"
end

#disabled_actions(*actions) ⇒ Object

Allows one to disable any number of default actions. Accepts the following parameters:

  • actions - A variable list of REST action symbols you wish to disable. You may use any of the following: index, show, new, create, edit, update, and/or destroy.



31
32
33
34
# File 'lib/class_methods.rb', line 31

def disabled_actions *actions
  actions.uniq!
  actions.each {|action| undef_method(action)}
end

#resource_options(options = {}) ⇒ Object

  • parent_key - The ID key of the parent resource (for nested resources only). Defaults to: <belongs_to name>_id

  • parent_value - The ID value of the parent resource (for nested resources only). Defaults to the record id.

  • parent_resource_method - The instance method to call for acquiring child resource(s) of the parent (for nested resources only). Example: A post with many comments would be post.comments. Defaults to child resource name.

  • name - The resource name. Defaults to the controller name.

  • label - The resource label. Defaults to the controller name with capitalization.

  • controller - The controller class. Defaults to the current class. You should not need to change this.

  • model - The model class. Defaults to a model of the same name as the controller (minus namespace, suffix, and pluralization of course).

  • record - The record (new or found) related to the resource.

  • namespaces - The namespaces (if any) for routing. Defaults to whatever namespace your controller is using.

  • show_partial - The show partial. Defaults to “/<namspace(s)>/<controller name>/_show”.

  • new_or_edit_partial - The new or edit partial. Defaults to “/<namspace(s)>/<controller name>/_new_or_edit”.



24
25
26
27
# File 'lib/class_methods.rb', line 24

def resource_options options = {}
  @resource_options = options
  class_eval "class << self; attr_reader :resource_options end"
end