Class: Orange::ModelResource
- Inherits:
-
RoutableResource
- Object
- Resource
- RoutableResource
- Orange::ModelResource
- Defined in:
- lib/orange-core/resources/model_resource.rb
Instance Attribute Summary collapse
-
#model_class ⇒ Object
Returns the value of attribute model_class.
Class Method Summary collapse
-
.expose(*args) ⇒ Object
Add methods to the list of exposed actions.
-
.expose_only(*args) ⇒ Object
Add an exclusive list of exposed actions.
- .listable(*args) ⇒ Object
-
.nests_in(*args) ⇒ Object
Allows the resource to nest inside other resources.
-
.nests_many(*args) ⇒ Object
Allows the resource to nest other resources.
-
.nests_one(*args) ⇒ Object
Allows the resource to nest other resources.
-
.new(*args, &block) ⇒ Object
Overrides the instantiation of new Resource object to set instance model class to the class-level model class defined by #use.
-
.use(my_model_class) ⇒ Object
Tells the Model resource which Carton class to scaffold.
Instance Method Summary collapse
- #afterDelete(packet, obj, opts = {}) ⇒ Object
-
#afterNew(packet, obj, opts = {}) ⇒ Object
A callback for after a new item is created.
- #afterSave(packet, obj, opts = {}) ⇒ Object
- #beforeDelete(packet, obj, opts = {}) ⇒ Object
-
#beforeNew(packet, opts = {}) ⇒ Object
A callback for before a new item is created.
- #beforeSave(packet, obj, opts = {}) ⇒ Object
-
#create(packet, *opts) ⇒ Object
Calls #do_view with :create mode.
-
#delete(packet, opts = {}) ⇒ Object
Deletes an object specified by packet, then reroutes to main.
-
#do_list_view(packet, mode, *args) ⇒ String
Renders a view, with all options set for haml to access.
-
#do_view(packet, mode, *args) ⇒ String
Renders a view, with all options set for haml to access.
-
#edit(packet, *opts) ⇒ Object
Calls #do_view with :edit mode.
-
#exposed(packet) ⇒ Object
Exposed method for helping the RestfulRouter class.
-
#find_list(packet, mode) ⇒ Enumerable
Returns a list of all objects found by the model class If none, returns an empty array.
-
#find_one(packet, mode, id = false) ⇒ Object
Returns a single object found by the model class, given an id.
-
#index(packet, *opts) ⇒ Object
Calls #do_list_view with :list mode.
-
#list(packet, *opts) ⇒ Object
Calls #do_list_view with :list mode.
- #nested_in ⇒ Object
- #nests ⇒ Object
-
#new(packet, opts = {}) ⇒ Object
Creates a new model object and saves it (if a post), then reroutes to the main page.
-
#onDelete(packet, obj, opts = {}) ⇒ Object
Delete object.
-
#onNew(packet, opts = {}) ⇒ Object
A callback for the actual new item event.
- #onSave(packet, obj, opts = {}) ⇒ Object
- #params_parse(packet, mode, params) ⇒ Object
-
#save(packet, opts = {}) ⇒ Object
Saves updates to an object specified by packet, then reroutes to main.
-
#show(packet, *opts) ⇒ Object
Calls #do_view with :show mode.
-
#table_row(packet, *opts) ⇒ Object
Calls #do_view with :table_row mode.
-
#view(packet, opts = {}) ⇒ Object
Views a packet by calling method defined as opts.
-
#view_opts(packet, mode, is_list, *args) ⇒ Hash
Returns the options for including in template rendering.
-
#viewable(packet, mode, opts = {}) ⇒ Object
Allows undefined methods to be viewed with the standard do_view method They must be added to the viewable or listable actions by calling #self.viewable or #self.listable with a set of functions to be viewable.
Methods inherited from RoutableResource
Methods inherited from Resource
#afterLoad, call_me, #find_extras, #init, #initialize, #options, #orange, #orange_name, #routable, set_orange, #set_orange, viewable
Methods included from ClassInheritableAttributes
#cattr_accessor, #cattr_reader, #cattr_writer, eval_in_accessor_module, fetch_value, store_value
Constructor Details
This class inherits a constructor from Orange::Resource
Instance Attribute Details
#model_class ⇒ Object
Returns the value of attribute model_class.
8 9 10 |
# File 'lib/orange-core/resources/model_resource.rb', line 8 def model_class @model_class end |
Class Method Details
.expose(*args) ⇒ Object
Add methods to the list of exposed actions.
294 295 296 297 298 |
# File 'lib/orange-core/resources/model_resource.rb', line 294 def self.expose(*args) self.exposed_actions ||= {:all => [:show, :list], :admin => :all, :orange => :all} self.exposed_actions.merge!(args.) self.exposed_actions[:all].concat(args) end |
.expose_only(*args) ⇒ Object
Add an exclusive list of exposed actions. List is cleared out beforehand
301 302 303 304 305 |
# File 'lib/orange-core/resources/model_resource.rb', line 301 def self.expose_only(*args) self.exposed_actions = {:all => []} self.exposed_actions.merge!(args.) self.exposed_actions[:all].concat(args) end |
.listable(*args) ⇒ Object
17 18 19 20 |
# File 'lib/orange-core/resources/model_resource.rb', line 17 def self.listable(*args) self.listable_actions ||= [] args.each{|arg| self.listable_actions << arg} end |
.nests_in(*args) ⇒ Object
Allows the resource to nest inside other resources
45 46 47 48 |
# File 'lib/orange-core/resources/model_resource.rb', line 45 def self.nests_in(*args) self.nested_in ||= [] args.each{|arg| self.nested_in << arg} end |
.nests_many(*args) ⇒ Object
Allows the resource to nest other resources
29 30 31 32 |
# File 'lib/orange-core/resources/model_resource.rb', line 29 def self.nests_many(*args) self.nests ||= {} args.each{|arg| self.nests[arg] = :many} end |
.nests_one(*args) ⇒ Object
Allows the resource to nest other resources
35 36 37 38 |
# File 'lib/orange-core/resources/model_resource.rb', line 35 def self.nests_one(*args) self.nests ||= {} args.each{|arg| self.nests[arg] = :one} end |
.new(*args, &block) ⇒ Object
Overrides the instantiation of new Resource object to set instance model class to the class-level model class defined by #use
56 57 58 59 60 61 62 63 64 |
# File 'lib/orange-core/resources/model_resource.rb', line 56 def self.new(*args, &block) self.exposed_actions ||= {:all => [:show, :list], :admin => [:all], :orange => [:all]} self.listable_actions ||= [] self.nested_in ||= [] self.nests ||= {} me = super(*args, &block) me.model_class = self.model_class me end |
.use(my_model_class) ⇒ Object
Tells the Model resource which Carton class to scaffold
24 25 26 |
# File 'lib/orange-core/resources/model_resource.rb', line 24 def self.use(my_model_class) self.model_class = my_model_class end |
Instance Method Details
#afterDelete(packet, obj, opts = {}) ⇒ Object
224 225 |
# File 'lib/orange-core/resources/model_resource.rb', line 224 def afterDelete(packet, obj, opts = {}) end |
#afterNew(packet, obj, opts = {}) ⇒ Object
A callback for after a new item is created
196 197 |
# File 'lib/orange-core/resources/model_resource.rb', line 196 def afterNew(packet, obj, opts = {}) end |
#afterSave(packet, obj, opts = {}) ⇒ Object
254 255 |
# File 'lib/orange-core/resources/model_resource.rb', line 254 def afterSave(packet, obj, opts = {}) end |
#beforeDelete(packet, obj, opts = {}) ⇒ Object
215 216 217 |
# File 'lib/orange-core/resources/model_resource.rb', line 215 def beforeDelete(packet, obj, opts = {}) true end |
#beforeNew(packet, opts = {}) ⇒ Object
A callback for before a new item is created
189 190 191 |
# File 'lib/orange-core/resources/model_resource.rb', line 189 def beforeNew(packet, opts = {}) true end |
#beforeSave(packet, obj, opts = {}) ⇒ Object
246 247 248 |
# File 'lib/orange-core/resources/model_resource.rb', line 246 def beforeSave(packet, obj, opts = {}) true end |
#create(packet, *opts) ⇒ Object
Calls #do_view with :create mode
271 272 273 |
# File 'lib/orange-core/resources/model_resource.rb', line 271 def create(packet, *opts) do_view(packet, :create, *opts) end |
#delete(packet, opts = {}) ⇒ Object
Deletes an object specified by packet, then reroutes to main. The request must come in as a delete. Rack::MethodOverride can be used to do this.
202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/orange-core/resources/model_resource.rb', line 202 def delete(packet, opts = {}) no_reroute = opts.delete(:no_reroute) if packet.request.delete? || !opts.blank? id = opts.delete(:resource_id) || packet['route.resource_id'] m = model_class.get(packet['route.resource_id']) before = beforeDelete(packet, m, opts) onDelete(packet, m, opts) if m && before afterDelete(packet, m, opts) if before orange.fire(:model_resource_deleted, packet, {:resource_id => id, :resource => @my_orange_name}) end packet.reroute(@my_orange_name, :orange) unless (packet.request.xhr? || no_reroute) end |
#do_list_view(packet, mode, *args) ⇒ String
Renders a view, with all options set for haml to access. Same as do_view, but calls #view_opts with is_list set to true to generate the haml options.
114 115 116 117 |
# File 'lib/orange-core/resources/model_resource.rb', line 114 def do_list_view(packet, mode, *args) haml_opts = view_opts(packet, mode, true, *args) orange[:parser].haml("#{mode.to_s}.haml", packet, haml_opts) end |
#do_view(packet, mode, *args) ⇒ String
Renders a view, with all options set for haml to access. Calls #view_opts to generate the haml options.
103 104 105 106 |
# File 'lib/orange-core/resources/model_resource.rb', line 103 def do_view(packet, mode, *args) haml_opts = view_opts(packet, mode, false, *args) orange[:parser].haml("#{mode.to_s}.haml", packet, haml_opts) end |
#edit(packet, *opts) ⇒ Object
Calls #do_view with :edit mode
265 266 267 |
# File 'lib/orange-core/resources/model_resource.rb', line 265 def edit(packet, *opts) do_view(packet, :edit, *opts) end |
#exposed(packet) ⇒ Object
Exposed method for helping the RestfulRouter class.
308 309 310 311 312 313 314 315 |
# File 'lib/orange-core/resources/model_resource.rb', line 308 def exposed(packet) self.class.exposed_actions ||= {:all => [:show, :list], :admin => :all, :orange => :all} all = self.class.exposed_actions[:all] all = [all] unless all.is_a?(Array) context = self.class.exposed_actions[packet['route.context']] context = [context] unless context.is_a?(Array) all+context end |
#find_list(packet, mode) ⇒ Enumerable
Returns a list of all objects found by the model class If none, returns an empty array
156 157 158 |
# File 'lib/orange-core/resources/model_resource.rb', line 156 def find_list(packet, mode) model_class.all || [] end |
#find_one(packet, mode, id = false) ⇒ Object
Returns a single object found by the model class, given an id. If id isn’t given, we return false.
145 146 147 148 |
# File 'lib/orange-core/resources/model_resource.rb', line 145 def find_one(packet, mode, id = false) return false unless id model_class.get(id) end |
#index(packet, *opts) ⇒ Object
Calls #do_list_view with :list mode.
289 290 291 |
# File 'lib/orange-core/resources/model_resource.rb', line 289 def index(packet, *opts) do_list_view(packet, :list, *opts) end |
#list(packet, *opts) ⇒ Object
Calls #do_list_view with :list mode
283 284 285 |
# File 'lib/orange-core/resources/model_resource.rb', line 283 def list(packet, *opts) do_list_view(packet, :list, *opts) end |
#nested_in ⇒ Object
50 51 52 |
# File 'lib/orange-core/resources/model_resource.rb', line 50 def nested_in self.class.nested_in end |
#nests ⇒ Object
40 41 42 |
# File 'lib/orange-core/resources/model_resource.rb', line 40 def nests self.class.nests || {} end |
#new(packet, opts = {}) ⇒ Object
Creates a new model object and saves it (if a post), then reroutes to the main page
162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/orange-core/resources/model_resource.rb', line 162 def new(packet, opts = {}) no_reroute = opts.delete(:no_reroute) if packet.request.post? || !opts.blank? params = opts.with_defaults(opts.delete(:params) || packet.request.params[@my_orange_name.to_s] || {}) params = params_parse(packet, :new, params) before = beforeNew(packet, params) obj = onNew(packet, params) if before afterNew(packet, obj, params) if before obj.save if obj && before end packet.reroute(@my_orange_name, :orange) unless (packet.request.xhr? || no_reroute) obj || false end |
#onDelete(packet, obj, opts = {}) ⇒ Object
Delete object
220 221 222 |
# File 'lib/orange-core/resources/model_resource.rb', line 220 def onDelete(packet, obj, opts = {}) obj.destroy end |
#onNew(packet, opts = {}) ⇒ Object
A callback for the actual new item event
183 184 185 |
# File 'lib/orange-core/resources/model_resource.rb', line 183 def onNew(packet, opts = {}) model_class.new(opts) end |
#onSave(packet, obj, opts = {}) ⇒ Object
250 251 252 |
# File 'lib/orange-core/resources/model_resource.rb', line 250 def onSave(packet, obj, opts = {}) obj.update(opts) end |
#params_parse(packet, mode, params) ⇒ Object
176 177 178 179 180 |
# File 'lib/orange-core/resources/model_resource.rb', line 176 def params_parse(packet, mode, params) props = model_class.form_props(packet['route.context'], :any) params.each{|k,v| params[k] = nil if(k.to_s =~ /_id$/ && v.blank?)} params end |
#save(packet, opts = {}) ⇒ Object
Saves updates to an object specified by packet, then reroutes to main
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/orange-core/resources/model_resource.rb', line 229 def save(packet, opts = {}) no_reroute = opts.delete(:no_reroute) if packet.request.post? || !opts.blank? my_id = opts.delete(:resource_id) || packet['route.resource_id'] m = opts.delete(:model) || model_class.get(my_id) params = opts.with_defaults(opts.delete(:params) || packet.request.params[@my_orange_name.to_s] || {}) params = params_parse(packet, :save, params) if m before = beforeSave(packet, m, params) onSave(packet, m, params) if before afterSave(packet, m, params) if before end end packet.reroute(@my_orange_name, :orange) unless (packet.request.xhr? || no_reroute) m || false end |
#show(packet, *opts) ⇒ Object
Calls #do_view with :show mode
259 260 261 |
# File 'lib/orange-core/resources/model_resource.rb', line 259 def show(packet, *opts) do_view(packet, :show, *opts) end |
#table_row(packet, *opts) ⇒ Object
Calls #do_view with :table_row mode
277 278 279 |
# File 'lib/orange-core/resources/model_resource.rb', line 277 def table_row(packet, *opts) do_view(packet, :table_row, *opts) end |
#view(packet, opts = {}) ⇒ Object
Views a packet by calling method defined as opts. Defaults mode to show or list, if it can’t find opts. Decision between show or list is determined by whether an id has been chosen. An id is set in opts, or extracted from the packet. Calling view is equivalent to calling a viewable method directly, view just sets up safe defaults so method missing errors are less likely.
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/orange-core/resources/model_resource.rb', line 73 def view(packet, opts = {}) resource_id = opts[:id] || packet['route.resource_id', false] mode = opts[:mode] || packet['route.resource_action'] || (resource_id ? :show : :index) if self.respond_to?(mode) self.__send__(mode, packet, opts) else viewable(packet, mode, opts) end end |
#view_opts(packet, mode, is_list, *args) ⇒ Hash
Returns the options for including in template rendering. All keys passed in the args array will automatically be local variables in the haml template. In addition, the props, resource, and model_name variables will be available.
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/orange-core/resources/model_resource.rb', line 128 def view_opts(packet, mode, is_list, *args) opts = args..with_defaults({:path => ''}) props = model_class.form_props(packet['route.context'], ((is_list || mode == :table_row) ? mode : :any)) resource_id = opts[:id] || packet['route.resource_id'] || false all_opts = {:props => props, :resource => self, :model_name => @my_orange_name}.merge!(opts) all_opts.with_defaults! :model => find_one(packet, mode, resource_id) unless is_list all_opts.with_defaults! :list => find_list(packet, mode) if is_list all_opts.with_defaults! find_extras(packet, mode) all_opts end |
#viewable(packet, mode, opts = {}) ⇒ Object
Allows undefined methods to be viewed with the standard do_view method They must be added to the viewable or listable actions by calling #self.viewable or #self.listable with a set of functions to be viewable
87 88 89 90 91 92 93 94 95 |
# File 'lib/orange-core/resources/model_resource.rb', line 87 def viewable(packet, mode, opts={}) if(self.class.viewable_actions.include?(mode)) do_view(packet, mode, opts) elsif(self.class.listable_actions.include?(mode)) do_list_view(packet, mode, opts) else '' end end |