Module: Cruddy::Controller::InstanceMethods
- Defined in:
- lib/cruddy/controller.rb
Instance Method Summary collapse
-
#create ⇒ Object
create action.
-
#destroy ⇒ Object
destroy action.
-
#edit ⇒ Object
edit action.
- #find_resource_collection ⇒ Object
- #find_resource_instance ⇒ Object
-
#index ⇒ Object
index action.
-
#new ⇒ Object
new action.
-
#resource ⇒ Object
get instance variable for singluar resource instance.
-
#resource=(resource) ⇒ Object
set instance variable for singluar resource instance.
-
#resource_class ⇒ Object
the class this controller manages.
-
#resource_collection_name ⇒ Object
the name of the resource collection.
-
#resource_instance_name ⇒ Object
the name of the singular resource.
-
#resource_params ⇒ Object
require the resource and permit all params by default.
-
#resource_path(resource) ⇒ Object
get the path to a singular resource.
-
#resources ⇒ Object
get instance variable for collection of resource instances.
-
#resources=(collection) ⇒ Object
set instance variable for collection of resource instances.
-
#resources_path ⇒ Object
get the path to index resources.
-
#show ⇒ Object
show action.
-
#update ⇒ Object
update action.
Instance Method Details
#create ⇒ Object
create action
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/cruddy/controller.rb', line 84 def create self.resource = resource_class.new(resource_params) if resource.save redirect_to resource_path(resource) else respond_with(resource) do |format| format.html { render :new } end end end |
#destroy ⇒ Object
destroy action
116 117 118 119 120 |
# File 'lib/cruddy/controller.rb', line 116 def destroy find_resource_instance resource.destroy redirect_to resources_path end |
#edit ⇒ Object
edit action
97 98 99 100 |
# File 'lib/cruddy/controller.rb', line 97 def edit find_resource_instance respond_with resource end |
#find_resource_collection ⇒ Object
61 62 63 |
# File 'lib/cruddy/controller.rb', line 61 def find_resource_collection self.resources = resource_class.all end |
#find_resource_instance ⇒ Object
57 58 59 |
# File 'lib/cruddy/controller.rb', line 57 def find_resource_instance self.resource = resource_class.find(params[:id]) end |
#index ⇒ Object
index action
66 67 68 69 |
# File 'lib/cruddy/controller.rb', line 66 def index find_resource_collection respond_with resources end |
#new ⇒ Object
new action
78 79 80 81 |
# File 'lib/cruddy/controller.rb', line 78 def new self.resource = resource_class.new respond_with resource end |
#resource ⇒ Object
get instance variable for singluar resource instance
23 24 25 |
# File 'lib/cruddy/controller.rb', line 23 def resource instance_variable_get("@#{resource_instance_name}") end |
#resource=(resource) ⇒ Object
set instance variable for singluar resource instance
28 29 30 |
# File 'lib/cruddy/controller.rb', line 28 def resource=(resource) instance_variable_set("@#{resource_instance_name}", resource) end |
#resource_class ⇒ Object
the class this controller manages
8 9 10 |
# File 'lib/cruddy/controller.rb', line 8 def resource_class controller_name.classify.constantize end |
#resource_collection_name ⇒ Object
the name of the resource collection
13 14 15 |
# File 'lib/cruddy/controller.rb', line 13 def resource_collection_name controller_name end |
#resource_instance_name ⇒ Object
the name of the singular resource
18 19 20 |
# File 'lib/cruddy/controller.rb', line 18 def resource_instance_name controller_name.singularize end |
#resource_params ⇒ Object
require the resource and permit all params by default
43 44 45 |
# File 'lib/cruddy/controller.rb', line 43 def resource_params params.require(resource_instance_name).permit! end |
#resource_path(resource) ⇒ Object
get the path to a singular resource
48 49 50 |
# File 'lib/cruddy/controller.rb', line 48 def resource_path(resource) send("#{resource_instance_name}_path", resource) end |
#resources ⇒ Object
get instance variable for collection of resource instances
33 34 35 |
# File 'lib/cruddy/controller.rb', line 33 def resources instance_variable_get("@#{resource_collection_name}") end |
#resources=(collection) ⇒ Object
set instance variable for collection of resource instances
38 39 40 |
# File 'lib/cruddy/controller.rb', line 38 def resources=(collection) instance_variable_set("@#{resource_collection_name}", collection) end |
#resources_path ⇒ Object
get the path to index resources
53 54 55 |
# File 'lib/cruddy/controller.rb', line 53 def resources_path send("#{resource_collection_name}_path") end |
#show ⇒ Object
show action
72 73 74 75 |
# File 'lib/cruddy/controller.rb', line 72 def show find_resource_instance respond_with resource end |
#update ⇒ Object
update action
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/cruddy/controller.rb', line 103 def update find_resource_instance if resource.update_attributes(resource_params) redirect_to resource_path(resource) else respond_with(resource) do |format| format.html { render :edit } end end end |