Class: Orange::ModelResource

Inherits:
RoutableResource show all
Defined in:
lib/orange-core/resources/model_resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RoutableResource

#routable, #route

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_classObject

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.extract_options!)
  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.extract_options!)
  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

Parameters:

  • my_model_class (Orange::Carton)

    class name of the 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

Parameters:



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

Parameters:



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

Parameters:



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.

Parameters:



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.

Parameters:

  • packet (Orange::Packet)

    the packet we are returning a view for

  • mode (Symbol)

    the mode we are trying to view (used to find template name)

  • args (optional, Array)

    the args array

Returns:

  • (String)

    haml parsed string to be placed in packet by #route



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.

Parameters:

  • packet (Orange::Packet)

    the packet we are returning a view for

  • mode (Symbol)

    the mode we are trying to view (used to find template name)

  • args (optional, Array)

    the args array

Returns:

  • (String)

    haml parsed string to be placed in packet by #route



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

Parameters:



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

Parameters:

  • packet (Orange::Packet)

    the packet we are returning a view for

  • mode (Symbol)

    the mode we are trying to view (used to find template name)

  • id (Numeric)

    the id to lookup on the model class

Returns:

  • (Enumerable)

    returns a collection of objects of type set by #use



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.

Parameters:

  • packet (Orange::Packet)

    the packet we are returning a view for

  • mode (Symbol)

    the mode we are trying to view (used to find template name)

  • id (Numeric) (defaults to: false)

    the id to lookup on the model class

Returns:

  • (Object)

    returns an object of type set by #use, if one found with same id



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.

Parameters:



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

Parameters:



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_inObject



50
51
52
# File 'lib/orange-core/resources/model_resource.rb', line 50

def nested_in
  self.class.nested_in
end

#nestsObject



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

Parameters:



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

Parameters:



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

Parameters:



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

Parameters:



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.

Parameters:

  • packet (Orange::Packet)

    the packet calling view on this resource



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.

Parameters:

  • packet (Orange::Packet)

    the packet we are returning a view for

  • mode (Symbol)

    the mode we are trying to view (used to find template name)

  • is_list (boolean)

    whether we want a list or not (view_opts will automatically look up a single object or a list of objects, so we need to know which)

  • args (optional, Array)

    the args array

Returns:

  • (Hash)

    hash of options to be used



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.extract_options!.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