Module: ResourcesController::InstanceMethods

Defined in:
lib/resources_controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(controller) ⇒ Object



559
560
561
# File 'lib/resources_controller.rb', line 559

def self.included(controller)
  controller.send :hide_action, *instance_methods
end

Instance Method Details

#enclosing_collection_resourcesObject

returns an array of the collection (non singleton) enclosing resources, this is used for generating routes.



634
635
636
# File 'lib/resources_controller.rb', line 634

def enclosing_collection_resources
  @enclosing_collection_resources ||= []
end

#enclosing_resourceObject

returns the immediately enclosing resource



608
609
610
# File 'lib/resources_controller.rb', line 608

def enclosing_resource
  enclosing_resources.last
end

#enclosing_resource_nameObject

returns the name of the immediately enclosing resource



613
614
615
# File 'lib/resources_controller.rb', line 613

def enclosing_resource_name
  @enclosing_resource_name
end

#enclosing_resourcesObject

returns an array of the controller’s enclosing (nested in) resources



629
630
631
# File 'lib/resources_controller.rb', line 629

def enclosing_resources
  @enclosing_resources ||= []
end

#name_prefixObject



567
568
569
# File 'lib/resources_controller.rb', line 567

def name_prefix
  @name_prefix ||= ''
end

#resourceObject

returns the controller’s current resource.



587
588
589
# File 'lib/resources_controller.rb', line 587

def resource
  instance_variable_get("@#{resource_name}")
end

#resource=(record) ⇒ Object

sets the controller’s current resource, and decorates the object with a save hook, so we know if it’s been saved



593
594
595
# File 'lib/resources_controller.rb', line 593

def resource=(record)
  instance_variable_set("@#{resource_name}", record)
end

#resource_classObject

returns the controller’s resource class



582
583
584
# File 'lib/resources_controller.rb', line 582

def resource_class
  resource_specification.klass
end

#resource_nameObject

name of the singular resource



572
573
574
# File 'lib/resources_controller.rb', line 572

def resource_name
  resource_specification.name
end

#resource_saved?Boolean

NOTE: This method is overly complicated and unecessary. It’s much clearer just to keep track of record saves yourself, this is here for BC. For an example of how it should be done look at the actions module in github.com/ianwhite/response_for_rc

Has the resource been saved successfully?, if no save has been attempted, save the record and return the result

This method uses the @resource_saved tracking var, or the model’s state itself if that is not available (which means if you do resource.update_attributes, then this method will return the correct result)

Returns:

  • (Boolean)


648
649
650
651
652
# File 'lib/resources_controller.rb', line 648

def resource_saved?
  save_resource if @resource_saved.nil? && !resource.validation_attempted?
  @resource_saved = resource.saved? if @resource_saved.nil?
  @resource_saved
end

#resource_serviceObject

returns the resource service for the controller - this will be lazilly created to a ResourceService, or a SingletonResourceService (if :singleton => true)



619
620
621
# File 'lib/resources_controller.rb', line 619

def resource_service
  @resource_service ||= resource_specification.singleton? ? SingletonResourceService.new(self) : ResourceService.new(self)
end

#resource_service=(service) ⇒ Object



563
564
565
# File 'lib/resources_controller.rb', line 563

def resource_service=(service)
  @resource_service = service
end

#resource_specificationObject

returns the instance resource_specification



624
625
626
# File 'lib/resources_controller.rb', line 624

def resource_specification
  self.class.resource_specification
end

#resourcesObject

returns the controller’s current resources collection



598
599
600
# File 'lib/resources_controller.rb', line 598

def resources
  instance_variable_get("@#{resources_name}")
end

#resources=(collection) ⇒ Object

sets the controller’s current resource collection



603
604
605
# File 'lib/resources_controller.rb', line 603

def resources=(collection)
  instance_variable_set("@#{resources_name}", collection)
end

#resources_nameObject

name of the resource collection



577
578
579
# File 'lib/resources_controller.rb', line 577

def resources_name
  @resources_name ||= resource_specification.name.pluralize
end

#save_resourceObject

NOTE: it’s clearer to just keep track of record saves yourself, this is here for BC See the comment on #resource_saved?

Save the resource, and keep track of the result



660
661
662
# File 'lib/resources_controller.rb', line 660

def save_resource
  @resource_saved = resource.save
end