Module: ResourcesController::InstanceMethods
- Defined in:
- lib/resources_controller.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#enclosing_collection_resources ⇒ Object
returns an array of the collection (non singleton) enclosing resources, this is used for generating routes.
-
#enclosing_resource ⇒ Object
returns the immediately enclosing resource.
-
#enclosing_resource_name ⇒ Object
returns the name of the immediately enclosing resource.
-
#enclosing_resources ⇒ Object
returns an array of the controller’s enclosing (nested in) resources.
- #name_prefix ⇒ Object
-
#resource ⇒ Object
returns the controller’s current resource.
-
#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.
-
#resource_class ⇒ Object
returns the controller’s resource class.
-
#resource_name ⇒ Object
name of the singular resource.
-
#resource_saved? ⇒ Boolean
NOTE: This method is overly complicated and unecessary.
-
#resource_service ⇒ Object
returns the resource service for the controller - this will be lazilly created to a ResourceService, or a SingletonResourceService (if :singleton => true).
- #resource_service=(service) ⇒ Object
-
#resource_specification ⇒ Object
returns the instance resource_specification.
-
#resources ⇒ Object
returns the controller’s current resources collection.
-
#resources=(collection) ⇒ Object
sets the controller’s current resource collection.
-
#resources_name ⇒ Object
name of the resource collection.
-
#save_resource ⇒ Object
NOTE: it’s clearer to just keep track of record saves yourself, this is here for BC See the comment on #resource_saved?.
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_resources ⇒ Object
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_resource ⇒ Object
returns the immediately enclosing resource
608 609 610 |
# File 'lib/resources_controller.rb', line 608 def enclosing_resource enclosing_resources.last end |
#enclosing_resource_name ⇒ Object
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_resources ⇒ Object
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_prefix ⇒ Object
567 568 569 |
# File 'lib/resources_controller.rb', line 567 def name_prefix @name_prefix ||= '' end |
#resource ⇒ Object
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_class ⇒ Object
returns the controller’s resource class
582 583 584 |
# File 'lib/resources_controller.rb', line 582 def resource_class resource_specification.klass end |
#resource_name ⇒ Object
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)
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_service ⇒ Object
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_specification ⇒ Object
returns the instance resource_specification
624 625 626 |
# File 'lib/resources_controller.rb', line 624 def resource_specification self.class.resource_specification end |
#resources ⇒ Object
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_name ⇒ Object
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_resource ⇒ Object
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 |