Module: MerbResourceScope::Controller::ScopedResourceMixin::InstanceMethods

Defined in:
lib/merb-resource-scope/controller/scoped_resource_mixin.rb

Instance Method Summary collapse

Instance Method Details

#_build_resource_scopeObject

the before filter, when this before filter is run; it will build the resource_scope



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/merb-resource-scope/controller/scoped_resource_mixin.rb', line 79

def _build_resource_scope
  if specifications = request.resource_specifications
    @_current_specification = specifications.pop
    @_enclosing_specifications = specifications
    @_resources_with_specs = []
  
    # if we dont want to load all the enclosing resource and resources
    # we can specify a depth to which build the the scope from 
    if scope_depth = @_current_specification.scope_depth
      @_enclosing_specifications.slice!(0..scope_depth-1)
    end
  
    _generate_enclosing_scope
    @resource_scope ||= MerbResourceScope::Controller::ScopedResourceMixin::ResourceScope.new(self)
  end
end

#current_resource=(resource) ⇒ Object

we set the current_resource to have the current specification resource_name method



168
169
170
171
# File 'lib/merb-resource-scope/controller/scoped_resource_mixin.rb', line 168

def current_resource=(resource)
  instance_variable_set("@#{@_current_specification.resource_name}", resource)
  @current_resource = resource
end

#current_resources=(resources) ⇒ Object

we set the current_resource to have the current specification resource_name method



174
175
176
177
# File 'lib/merb-resource-scope/controller/scoped_resource_mixin.rb', line 174

def current_resources=(resources)
  instance_variable_set("@#{@_current_specification.association_method}", resources)
  @current_resources = resources
end

#enclosing_resourceObject



96
97
98
99
100
101
102
103
# File 'lib/merb-resource-scope/controller/scoped_resource_mixin.rb', line 96

def enclosing_resource
  return nil if @_enclosing_specifications.size == 0
  if @_resources_with_specs.size == @_enclosing_specifications.size+1
    @_resources_with_specs.slice(-2)[0]
  else
    @_resources_with_specs.last && @_resources_with_specs.last[0]      
  end
end

#find_resource(id = ) ⇒ Object

if you want to find a resource in another then this default way  then you can override in the controller,

Examples:

def find_resource
  @resource_scope.first :conditions => {:user_id => 1, :pending => false}
end

Parameters:

  • id (String) (defaults to: )

    |<Integer> the params to use to find a resource could be permalink



136
137
138
139
140
141
142
# File 'lib/merb-resource-scope/controller/scoped_resource_mixin.rb', line 136

def find_resource(id = params[@_current_specification.permalink])
  if find_method = @_current_specification.find_method
    find_method.is_a?(Proc) ? self.instance_eval(&find_method) : self.send(find_method)
  else
    @_current_specification.singleton ? @resource_scope : @resource_scope.first(:conditions => {@_current_specification.permalink => id})
  end
end

#find_resourcesObject

if you want to find resources in another way just override this in your controller;

Examples:

def find_resources
  @resource_scope.all :conditions => {:pending => true}
end


121
122
123
# File 'lib/merb-resource-scope/controller/scoped_resource_mixin.rb', line 121

def find_resources
  @resource_scope.all
end

#new_resource(attributes = (params[@_current_specification.resource_name] || {})) ⇒ Object

if you have other requirments on how you would like to create a new resource  then you can override this at the controller level, using the @resource_scope Also if you wish to have different attributes set you can just add them as well dont really want to go into it but I cannot use the proxy here as a send then a method call basically stops the dependent attributes from being set! strange but true

Examples:

def new_resource
   @resource_scope.different_new {:whatever => :egg}
end


156
157
158
159
160
161
162
163
164
165
# File 'lib/merb-resource-scope/controller/scoped_resource_mixin.rb', line 156

def new_resource(attributes = (params[@_current_specification.resource_name] || {}))
  if enclosing_resource
    if @_current_specification.singleton
      return @_current_specification.klass.new attributes.merge!(_enclosing_specification.association_method.to_sym => enclosing_resource)
    end
    enclosing_resource.send(@_current_specification.association_method).build attributes
  else
    @_current_specification.klass.new attributes
  end
end