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

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

.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



18
19
20
21
22
# File 'lib/orange-core/resources/model_resource.rb', line 18

def self.new(*args, &block)
  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



12
13
14
# File 'lib/orange-core/resources/model_resource.rb', line 12

def self.use(my_model_class)
  self.model_class = my_model_class
end

Instance Method Details

#afterDelete(packet, obj, opts = {}) ⇒ Object



157
158
# File 'lib/orange-core/resources/model_resource.rb', line 157

def afterDelete(packet, obj, opts = {})
end

#afterNew(packet, obj, opts = {}) ⇒ Object

A callback for after a new item is created

Parameters:



130
131
# File 'lib/orange-core/resources/model_resource.rb', line 130

def afterNew(packet, obj, opts = {})
end

#afterSave(packet, obj, opts = {}) ⇒ Object



186
187
# File 'lib/orange-core/resources/model_resource.rb', line 186

def afterSave(packet, obj, opts = {})
end

#beforeDelete(packet, obj, opts = {}) ⇒ Object



148
149
150
# File 'lib/orange-core/resources/model_resource.rb', line 148

def beforeDelete(packet, obj, opts = {})
  true
end

#beforeNew(packet, opts = {}) ⇒ Object

A callback for before a new item is created

Parameters:



123
124
125
# File 'lib/orange-core/resources/model_resource.rb', line 123

def beforeNew(packet, opts = {})
  true
end

#beforeSave(packet, obj, opts = {}) ⇒ Object



178
179
180
# File 'lib/orange-core/resources/model_resource.rb', line 178

def beforeSave(packet, obj, opts = {})
  true
end

#create(packet, *opts) ⇒ Object

Calls #do_view with :create mode

Parameters:



203
204
205
# File 'lib/orange-core/resources/model_resource.rb', line 203

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:



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/orange-core/resources/model_resource.rb', line 136

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
  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



55
56
57
58
# File 'lib/orange-core/resources/model_resource.rb', line 55

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



44
45
46
47
# File 'lib/orange-core/resources/model_resource.rb', line 44

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:



197
198
199
# File 'lib/orange-core/resources/model_resource.rb', line 197

def edit(packet, *opts)
  do_view(packet, :edit, *opts)
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



97
98
99
# File 'lib/orange-core/resources/model_resource.rb', line 97

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



86
87
88
89
# File 'lib/orange-core/resources/model_resource.rb', line 86

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:



221
222
223
# File 'lib/orange-core/resources/model_resource.rb', line 221

def index(packet, *opts)
  do_list_view(packet, :list, *opts)
end

#list(packet, *opts) ⇒ Object

Calls #do_list_view with :list mode

Parameters:



215
216
217
# File 'lib/orange-core/resources/model_resource.rb', line 215

def list(packet, *opts)
  do_list_view(packet, :list, *opts)
end

#new(packet, opts = {}) ⇒ Object

Creates a new model object and saves it (if a post), then reroutes to the main page

Parameters:



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/orange-core/resources/model_resource.rb', line 103

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] || {})
    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



153
154
155
# File 'lib/orange-core/resources/model_resource.rb', line 153

def onDelete(packet, obj, opts = {})
  obj.destroy
end

#onNew(packet, opts = {}) ⇒ Object

A callback for the actual new item event



117
118
119
# File 'lib/orange-core/resources/model_resource.rb', line 117

def onNew(packet, opts = {})
  model_class.new(opts)
end

#onSave(packet, obj, opts = {}) ⇒ Object



182
183
184
# File 'lib/orange-core/resources/model_resource.rb', line 182

def onSave(packet, obj, opts = {})
  obj.update(opts)
end

#save(packet, opts = {}) ⇒ Object

Saves updates to an object specified by packet, then reroutes to main

Parameters:



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/orange-core/resources/model_resource.rb', line 162

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] || {})
    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:



191
192
193
# File 'lib/orange-core/resources/model_resource.rb', line 191

def show(packet, *opts)
  do_view(packet, :show, *opts)
end

#table_row(packet, *opts) ⇒ Object

Calls #do_view with :table_row mode

Parameters:



209
210
211
# File 'lib/orange-core/resources/model_resource.rb', line 209

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



31
32
33
34
35
36
# File 'lib/orange-core/resources/model_resource.rb', line 31

def view(packet, opts = {})
  resource_id = opts[:id] || packet['route.resource_id', false]
  mode = opts[:mode] || packet['route.resource_action'] || 
    (resource_id ? :show : :index)
  self.__send__(mode, packet, opts)
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



69
70
71
72
73
74
75
76
77
78
# File 'lib/orange-core/resources/model_resource.rb', line 69

def view_opts(packet, mode, is_list, *args)
  opts = args.extract_options!.with_defaults({:path => ''})
  props = model_class.form_props(packet['route.context'])
  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