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.
-
#edit_resource_path(resource) ⇒ Object
get the path to edit a singular resource.
-
#get_resource_collection ⇒ Object
get the resource collection.
-
#get_resource_instance ⇒ Object
get the resource instance.
-
#index ⇒ Object
index action.
-
#initialize_resource_instance(params = nil) ⇒ Object
intiialize the resource instance (duh).
-
#load_resource_collection ⇒ Object
load the resource collection.
-
#load_resource_instance ⇒ Object
load the resource instance.
-
#new ⇒ Object
new action.
-
#new_resource_path ⇒ Object
get the path to create a new singular resource.
-
#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_collection_path ⇒ Object
get the path to the collection of resources, including the namespace.
-
#resource_instance_name ⇒ Object
the name of the singular resource.
-
#resource_instance_path ⇒ Object
get the path to a singular resource, including the namespace.
-
#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
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/cruddy/controller.rb', line 129 def create self.resource = initialize_resource_instance(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
158 159 160 161 |
# File 'lib/cruddy/controller.rb', line 158 def destroy resource.destroy redirect_to resources_path end |
#edit ⇒ Object
edit action
142 143 144 |
# File 'lib/cruddy/controller.rb', line 142 def edit respond_with resource end |
#edit_resource_path(resource) ⇒ Object
get the path to edit a singular resource
72 73 74 |
# File 'lib/cruddy/controller.rb', line 72 def edit_resource_path(resource) send("edit_#{resource_instance_path}", resource) end |
#get_resource_collection ⇒ Object
get the resource collection
102 103 104 |
# File 'lib/cruddy/controller.rb', line 102 def get_resource_collection resource_class.all end |
#get_resource_instance ⇒ Object
get the resource instance
87 88 89 |
# File 'lib/cruddy/controller.rb', line 87 def get_resource_instance resource_class.find(params[:id]) end |
#index ⇒ Object
index action
112 113 114 115 |
# File 'lib/cruddy/controller.rb', line 112 def index load_resource_collection respond_with resources end |
#initialize_resource_instance(params = nil) ⇒ Object
intiialize the resource instance (duh)
97 98 99 |
# File 'lib/cruddy/controller.rb', line 97 def initialize_resource_instance(params=nil) resource_class.new(params) end |
#load_resource_collection ⇒ Object
load the resource collection
107 108 109 |
# File 'lib/cruddy/controller.rb', line 107 def load_resource_collection self.resources = get_resource_collection end |
#load_resource_instance ⇒ Object
load the resource instance
92 93 94 |
# File 'lib/cruddy/controller.rb', line 92 def load_resource_instance self.resource = get_resource_instance end |
#new ⇒ Object
new action
123 124 125 126 |
# File 'lib/cruddy/controller.rb', line 123 def new self.resource = initialize_resource_instance respond_with resource end |
#new_resource_path ⇒ Object
get the path to create a new singular resource
77 78 79 |
# File 'lib/cruddy/controller.rb', line 77 def new_resource_path send("new_#{resource_instance_path}") end |
#resource ⇒ Object
get instance variable for singluar resource instance
30 31 32 |
# File 'lib/cruddy/controller.rb', line 30 def resource instance_variable_get("@#{resource_instance_name}") end |
#resource=(resource) ⇒ Object
set instance variable for singluar resource instance
35 36 37 |
# File 'lib/cruddy/controller.rb', line 35 def resource=(resource) instance_variable_set("@#{resource_instance_name}", resource) end |
#resource_class ⇒ Object
the class this controller manages
15 16 17 |
# File 'lib/cruddy/controller.rb', line 15 def resource_class controller_name.classify.constantize end |
#resource_collection_name ⇒ Object
the name of the resource collection
20 21 22 |
# File 'lib/cruddy/controller.rb', line 20 def resource_collection_name controller_name end |
#resource_collection_path ⇒ Object
get the path to the collection of resources, including the namespace
55 56 57 58 |
# File 'lib/cruddy/controller.rb', line 55 def resource_collection_path path_start = controller_path.gsub('/', '_') "#{path_start}_path" end |
#resource_instance_name ⇒ Object
the name of the singular resource
25 26 27 |
# File 'lib/cruddy/controller.rb', line 25 def resource_instance_name controller_name.singularize end |
#resource_instance_path ⇒ Object
get the path to a singular resource, including the namespace
61 62 63 64 |
# File 'lib/cruddy/controller.rb', line 61 def resource_instance_path path_start = controller_path.gsub('/', '_').singularize "#{path_start}_path" end |
#resource_params ⇒ Object
require the resource and permit all params by default
50 51 52 |
# File 'lib/cruddy/controller.rb', line 50 def resource_params params.require(resource_instance_name).permit! end |
#resource_path(resource) ⇒ Object
get the path to a singular resource
67 68 69 |
# File 'lib/cruddy/controller.rb', line 67 def resource_path(resource) send("#{resource_instance_path}", resource) end |
#resources ⇒ Object
get instance variable for collection of resource instances
40 41 42 |
# File 'lib/cruddy/controller.rb', line 40 def resources instance_variable_get("@#{resource_collection_name}") end |
#resources=(collection) ⇒ Object
set instance variable for collection of resource instances
45 46 47 |
# File 'lib/cruddy/controller.rb', line 45 def resources=(collection) instance_variable_set("@#{resource_collection_name}", collection) end |
#resources_path ⇒ Object
get the path to index resources
82 83 84 |
# File 'lib/cruddy/controller.rb', line 82 def resources_path send("#{resource_collection_path}") end |
#show ⇒ Object
show action
118 119 120 |
# File 'lib/cruddy/controller.rb', line 118 def show respond_with resource end |
#update ⇒ Object
update action
147 148 149 150 151 152 153 154 155 |
# File 'lib/cruddy/controller.rb', line 147 def update if resource.update_attributes(resource_params) redirect_to resource_path(resource) else respond_with(resource) do |format| format.html { render :edit } end end end |