Module: Cruddy::Controller::InstanceMethods

Defined in:
lib/cruddy/controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

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

#destroyObject

destroy action



158
159
160
161
# File 'lib/cruddy/controller.rb', line 158

def destroy
  resource.destroy
  redirect_to resources_path
end

#editObject

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_collectionObject

get the resource collection



102
103
104
# File 'lib/cruddy/controller.rb', line 102

def get_resource_collection
  resource_class.all
end

#get_resource_instanceObject

get the resource instance



87
88
89
# File 'lib/cruddy/controller.rb', line 87

def get_resource_instance
  resource_class.find(params[:id])
end

#indexObject

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_collectionObject

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_instanceObject

load the resource instance



92
93
94
# File 'lib/cruddy/controller.rb', line 92

def load_resource_instance
  self.resource = get_resource_instance
end

#newObject

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_pathObject

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

#resourceObject

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_classObject

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_nameObject

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_pathObject

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_nameObject

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_pathObject

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_paramsObject

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

#resourcesObject

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_pathObject

get the path to index resources



82
83
84
# File 'lib/cruddy/controller.rb', line 82

def resources_path
  send("#{resource_collection_path}")
end

#showObject

show action



118
119
120
# File 'lib/cruddy/controller.rb', line 118

def show
  respond_with resource
end

#updateObject

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