Module: Heimdallr::LegacyResource::ClassMethods

Defined in:
lib/heimdallr/legacy_resource.rb

Overview

Class methods for Resource. See also ActiveSupport::Concern.

Configuration collapse

Configuration collapse

Instance Attribute Details

#modelClass (readonly)

Returns the attached model class.

Returns:

  • (Class)


250
251
252
# File 'lib/heimdallr/legacy_resource.rb', line 250

def model
  @model
end

Instance Method Details

#resource_for(model, options = {}) ⇒ Object

Attaches this resource to a model.

Note that ActiveSupport before_filter replaces the list of actions for specified filter and not appends to it. For example, the following code will only run filter_a when bar action is invoked:

class FooController < ApplicationController
  before_filter :filter_a, :only => :foo
  before_filter :filter_a, :only => :bar

  def foo; end
  def bar; end
end

For convenience, you can pass additional actions to register with default filters in options. It is also possible to use append_before_filter.

Parameters:

  • model (Class)

    an ActiveRecord-derived model class

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :index (Array<Symbol>)

    Additional actions to be covered by Resource#load_all_resources.

  • :member (Array<Symbol>)

    Additional actions to be covered by Resource#load_one_resource.

  • :collection (Array<Symbol>)

    Additional actions to be covered by Resource#load_referenced_resources.



276
277
278
279
280
281
# File 'lib/heimdallr/legacy_resource.rb', line 276

def resource_for(model, options={})
  @model = model.to_s.camelize.constantize

  before_filter :load_all_resources,        only: [ :index ].concat(options[:all] || [])
  before_filter :load_referenced_resources, only: [ :show, :update, :destroy ].concat(options[:referenced] || [])
end