class AutoForme::Model

  1. lib/autoforme/model.rb
Superclass: Object

Wraps a specific model class

Constants

AUTOCOMPLETE_TYPES = [:show, :edit, :delete, :association, :mtm_edit].freeze  

Array of supported autocomplete types

DEFAULT_LIMIT = 25  

The default number of records to show on each browse/search results pages

DEFAULT_SUPPORTED_ACTIONS = [:browse, :new, :show, :edit, :delete, :search, :mtm_edit]  

The default supported actions for models.

DEFAULT_TABLE_CLASS = "table table-bordered table-striped"  

The default table class to use for browse/search results pages

VALID_CONSTANT_NAME_REGEXP = /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/.freeze  

Regexp for valid constant names, to prevent code execution.

Attributes

framework [R]

The AutoForme::Framework class tied to the current model

opts [R]

The options for the given model.

Public Class methods

for (framework, type, model_class, &block)

Create a new instance for the given model type and underlying model class tied to the given framework.

[show source]
   # File lib/autoforme/model.rb
25 def self.for(framework, type, model_class, &block)
26   model = AutoForme.model_class_for(type).new(model_class, framework)
27   model.instance_exec(&block) if block
28   model
29 end
new (model, framework)
[show source]
   # File lib/autoforme/model.rb
46 def initialize(model, framework)
47   @model = model
48   @framework = framework
49   @opts = {}
50 end

Public Instance methods

associated_model_class (assoc)

The AutoForme::Model instance associated to the given association.

[show source]
    # File lib/autoforme/model.rb
246 def associated_model_class(assoc)
247   framework.model_class(associated_class(assoc))
248 end
associated_object_display_name (assoc, request, obj)

A human readable string for the associated object.

[show source]
    # File lib/autoforme/model.rb
332 def associated_object_display_name(assoc, request, obj)
333   apply_name_method(column_options_for(:mtm_edit, request, assoc)[:name_method], obj, :mtm_edit, request)
334 end
autocomplete_options_for (type, request)
[show source]
    # File lib/autoforme/model.rb
226 def autocomplete_options_for(type, request)
227   return unless AUTOCOMPLETE_TYPES.include?(type)
228   framework_opts = framework.autocomplete_options_for(model, type, request)
229   model_opts = handle_proc(autocomplete_options, type, request)
230   if model_opts
231     (framework_opts || {}).merge(model_opts)
232   end
233 end
before_action_hook (type, request)

Run framework and model before_action hooks with type symbol and request.

[show source]
    # File lib/autoforme/model.rb
276 def before_action_hook(type, request)
277   if v = framework.before_action
278     v.call(type, request)
279   end
280   if v = before_action
281     v.call(type, request)
282   end
283 end
class_name ()

The name to display to the user for this model.

[show source]
    # File lib/autoforme/model.rb
236 def class_name
237   class_display_name || model.name
238 end
column_options_for (type, request, column)

The options to use for the given column and request. Instead of the model options overriding the framework options, they are merged together.

[show source]
    # File lib/autoforme/model.rb
100 def column_options_for(type, request, column)
101   framework_opts = case framework_opts = framework.column_options
102   when Proc, Method
103     framework_opts.call(model, column, type, request) || {}
104   else
105     extract_column_options(framework_opts, column, type, request)
106   end
107 
108   model_opts = case model_opts = column_options
109   when Proc, Method
110     model_opts.call(column, type, request) || {}
111   else
112     extract_column_options(model_opts, column, type, request)
113   end
114 
115   opts = framework_opts.merge(model_opts).dup
116 
117   if association?(column) && associated_model = associated_model_class(column)
118     if associated_model.autocomplete_options_for(:association, request) && !opts[:as] && association_type(column) == :one
119       opts[:type] = 'text'
120       opts[:class] = 'autoforme_autocomplete'
121       opts[:attr] = {'data-column'=>column, 'data-type'=>type}
122       opts[:name] = form_param_name(column)
123     else
124       unless opts[:name_method]
125         opts[:name_method] = lambda{|obj| associated_model.object_display_name(:association, request, obj)}
126       end
127 
128       case type
129       when :edit, :new, :search_form
130         unless opts[:options] || opts[:dataset]
131           opts[:dataset] = lambda{|ds| associated_model.apply_dataset_options(:association, request, ds)}
132         end
133       end
134     end
135   end
136 
137   case type
138   when :show, :search_form
139     opts[:required] = false unless opts.has_key?(:required)
140     if type == :search_form && opts[:as] == :textarea
141       opts.delete(:as)
142     end
143   end
144 
145   opts
146 end
column_value (type, request, obj, column)

The column value to display for the given object and column.

[show source]
    # File lib/autoforme/model.rb
251 def column_value(type, request, obj, column)
252   v = obj.send(column)
253   return if v.nil?
254   if association?(column) 
255     opts = column_options_for(type, request, column) 
256     case nm = opts[:name_method]
257     when Symbol, String
258       v = v.send(nm)
259     when nil
260     else
261       v = nm.call(v)
262     end
263   end
264   if v.is_a?(base_class)
265     v = default_object_display_name(v)
266   end
267   v
268 end
columns_for (type, request)
[show source]
   # File lib/autoforme/model.rb
94 def columns_for(type, request)
95   handle_proc(columns || framework.columns_for(model, type, request), type, request) || default_columns
96 end
default_object_display_name (obj)

A fallback for the display name for the object if none is configured.

[show source]
    # File lib/autoforme/model.rb
337 def default_object_display_name(obj)
338   if obj.respond_to?(:forme_name)
339     obj.forme_name
340   elsif obj.respond_to?(:name)
341     obj.name
342   else
343     primary_key_value(obj)
344   end.to_s
345 end
destroy (obj)

Destroy the given object, deleting it from the database.

[show source]
    # File lib/autoforme/model.rb
271 def destroy(obj)
272   obj.destroy
273 end
display_name_for ()
[show source]
    # File lib/autoforme/model.rb
200 def display_name_for
201   display_name || framework.display_name_for(model)
202 end
eager_for (type, request)
[show source]
    # File lib/autoforme/model.rb
160 def eager_for(type, request)
161   handle_proc(eager, type, request)
162 end
eager_graph_for (type, request)
[show source]
    # File lib/autoforme/model.rb
164 def eager_graph_for(type, request)
165   handle_proc(eager_graph, type, request)
166 end
edit_html_for (obj, column, type, request)
[show source]
    # File lib/autoforme/model.rb
152 def edit_html_for(obj, column, type, request)
153   handle_proc(edit_html || framework.edit_html_for(obj, column, type, request), obj, column, type, request)
154 end
filter_for ()
[show source]
    # File lib/autoforme/model.rb
168 def filter_for
169   filter || framework.filter_for(model)
170 end
form_attributes_for (type, request)
[show source]
    # File lib/autoforme/model.rb
176 def form_attributes_for(type, request)
177   framework.form_attributes_for(model, type, request).merge(handle_proc(form_attributes, type, request) || {})
178 end
form_options_for (type, request)
[show source]
    # File lib/autoforme/model.rb
180 def form_options_for(type, request)
181   framework.form_options_for(model, type, request).merge(handle_proc(form_options, type, request) || {})
182 end
hook (type, request, obj)

Run given hooks with the related object and request.

[show source]
    # File lib/autoforme/model.rb
286 def hook(type, request, obj)
287   if type.to_s =~ /before/
288     if v = framework.send(type)
289       v.call(obj, request)
290     end
291     if v = send(type)
292       v.call(obj, request)
293     end
294   else
295     if v = send(type)
296       v.call(obj, request)
297     end
298     if v = framework.send(type)
299       v.call(obj, request)
300     end
301   end
302 end
inline_mtm_assocs (request)

An array of many to many association symbols to handle inline on the edit forms.

[show source]
   # File lib/autoforme/model.rb
90 def inline_mtm_assocs(request)
91   normalize_mtm_associations(handle_proc(inline_mtm_associations || framework.inline_mtm_associations_for(model, request), request))
92 end
limit_for (type, request)
[show source]
    # File lib/autoforme/model.rb
196 def limit_for(type, request)
197   handle_proc(per_page || framework.limit_for(model, type, request), type, request) || DEFAULT_LIMIT
198 end
model ()

The underlying model class for the current model

[show source]
   # File lib/autoforme/model.rb
53 def model
54   if @model.is_a?(Class)
55     @model
56   elsif m = VALID_CONSTANT_NAME_REGEXP.match(@model)
57     Object.module_eval("::#{m[1]}", __FILE__, __LINE__)
58   else
59     raise Error, "invalid model for AutoForme::Model, not a class or valid constant name: #{@model.inspect}"
60   end
61 end
mtm_association_select_options (request)

An array of many to many association symbols to handle via a separate mtm_edit page.

[show source]
   # File lib/autoforme/model.rb
75 def mtm_association_select_options(request)
76   normalize_mtm_associations(handle_proc(mtm_associations || framework.mtm_associations_for(model, request), request))
77 end
new (params, request)

Create a new instance of the underlying model, setting defaults based on the params given.

[show source]
    # File lib/autoforme/model.rb
306 def new(params, request)
307   obj = model.new
308   if params
309     columns_for(:new, request).each do |col|
310       if association?(col)
311         col = association_key(col)
312       end
313       if v = params[col.to_s]
314         obj.send("#{col}=", v)
315       end
316     end
317   end
318   obj
319 end
object_display_name (type, request, obj)

A human readable string representing the object.

[show source]
    # File lib/autoforme/model.rb
327 def object_display_name(type, request, obj)
328   apply_name_method(display_name_for, obj, type, request).to_s
329 end
order_for (type, request)
[show source]
    # File lib/autoforme/model.rb
156 def order_for(type, request)
157   handle_proc(order || framework.order_for(model, type, request), type, request)
158 end
page_header_for (type, request)
[show source]
    # File lib/autoforme/model.rb
188 def page_header_for(type, request)
189   handle_proc(page_header || framework.page_header_for(model, type, request), type, request)
190 end
redirect_for ()
[show source]
    # File lib/autoforme/model.rb
172 def redirect_for
173   redirect || framework.redirect_for(model)
174 end
select_options (type, request)

An array of pairs for the select options to return for the given type.

[show source]
    # File lib/autoforme/model.rb
322 def select_options(type, request)
323   all_rows_for(type, request).map{|obj| [object_display_name(type, request, obj), primary_key_value(obj)]}
324 end
show_html_for (obj, column, type, request)
[show source]
    # File lib/autoforme/model.rb
148 def show_html_for(obj, column, type, request)
149   handle_proc(show_html || framework.show_html_for(obj, column, type, request), obj, column, type, request)
150 end
supported_action? (type, request)

Whether the given type of action is supported for this model.

[show source]
   # File lib/autoforme/model.rb
64 def supported_action?(type, request)
65   v = (handle_proc(supported_actions || framework.supported_actions_for(model, request), request) || DEFAULT_SUPPORTED_ACTIONS).include?(type)
66   if v && type == :mtm_edit
67     assocs = mtm_association_select_options(request)
68     assocs && !assocs.empty?
69   else
70     v
71   end
72 end
supported_mtm_edit? (assoc, request)

Whether an mtm_edit can be displayed for the given association

[show source]
   # File lib/autoforme/model.rb
80 def supported_mtm_edit?(assoc, request)
81   mtm_association_select_options(request).map(&:to_s).include?(assoc)
82 end
supported_mtm_update? (assoc, request)

Whether an mtm_update can occur for the given association

[show source]
   # File lib/autoforme/model.rb
85 def supported_mtm_update?(assoc, request)
86   supported_mtm_edit?(assoc, request) || inline_mtm_assocs(request).map(&:to_s).include?(assoc) 
87 end
table_class_for (type, request)
[show source]
    # File lib/autoforme/model.rb
192 def table_class_for(type, request)
193   handle_proc(table_class || framework.table_class_for(model, type, request), type, request) || DEFAULT_TABLE_CLASS
194 end