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_PAGINATION_STRATEGY = :offset  

The default pagination strategy to use. Offset is used by default, as a filter strategy requires an unambiguous order, which the library cannot guarantee.

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
30 def self.for(framework, type, model_class, &block)
31   model = AutoForme.model_class_for(type).new(model_class, framework)
32   model.instance_exec(&block) if block
33   model
34 end
new(model, framework)
[show source]
   # File lib/autoforme/model.rb
51 def initialize(model, framework)
52   @model = model
53   @framework = framework
54   @opts = {}
55 end

Public Instance methods

associated_model_class(assoc)

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

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

A human readable string for the associated object.

[show source]
    # File lib/autoforme/model.rb
345 def associated_object_display_name(assoc, request, obj)
346   apply_name_method(column_options_for(:mtm_edit, request, assoc)[:name_method], obj, :mtm_edit, request)
347 end
autocomplete_options_for(type, request)
[show source]
    # File lib/autoforme/model.rb
239 def autocomplete_options_for(type, request)
240   return unless AUTOCOMPLETE_TYPES.include?(type)
241   framework_opts = framework.autocomplete_options_for(model, type, request)
242   model_opts = handle_proc(autocomplete_options, type, request)
243   if model_opts
244     (framework_opts || {}).merge(model_opts)
245   end
246 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
289 def before_action_hook(type, request)
290   if v = framework.before_action
291     v.call(type, request)
292   end
293   if v = before_action
294     v.call(type, request)
295   end
296 end
class_name()

The name to display to the user for this model.

[show source]
    # File lib/autoforme/model.rb
249 def class_name
250   class_display_name || model.name
251 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
113 def column_options_for(type, request, column)
114   framework_opts = case framework_opts = framework.column_options
115   when Proc, Method
116     framework_opts.call(model, column, type, request) || {}
117   else
118     extract_column_options(framework_opts, column, type, request)
119   end
120 
121   model_opts = case model_opts = column_options
122   when Proc, Method
123     model_opts.call(column, type, request) || {}
124   else
125     extract_column_options(model_opts, column, type, request)
126   end
127 
128   opts = framework_opts.merge(model_opts).dup
129 
130   if association?(column) && associated_model = associated_model_class(column)
131     if associated_model.autocomplete_options_for(:association, request) && !opts[:as] && association_type(column) == :one
132       opts[:type] = 'text'
133       opts[:class] = 'autoforme_autocomplete'
134       opts[:attr] = {'data-column'=>column, 'data-type'=>type}
135       opts[:name] = form_param_name(column)
136     else
137       unless opts[:name_method]
138         opts[:name_method] = lambda{|obj| associated_model.object_display_name(:association, request, obj)}
139       end
140 
141       case type
142       when :edit, :new, :search_form
143         unless opts[:options] || opts[:dataset]
144           opts[:dataset] = lambda{|ds| associated_model.apply_dataset_options(:association, request, ds)}
145         end
146       end
147     end
148   end
149 
150   case type
151   when :show, :search_form
152     opts[:required] = false unless opts.has_key?(:required)
153     if type == :search_form && opts[:as] == :textarea
154       opts.delete(:as)
155     end
156   end
157 
158   opts
159 end
column_search_filter_for(dataset, column, value, request)
[show source]
    # File lib/autoforme/model.rb
107 def column_search_filter_for(dataset, column, value, request)
108   handle_proc(column_search_filter || framework.column_search_filter_for(model, dataset, column, value, request), dataset, column, value, request)
109 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
264 def column_value(type, request, obj, column)
265   v = obj.send(column)
266   return if v.nil?
267   if association?(column) 
268     opts = column_options_for(type, request, column) 
269     case nm = opts[:name_method]
270     when Symbol, String
271       v = v.send(nm)
272     when nil
273     else
274       v = nm.call(v)
275     end
276   end
277   if v.is_a?(base_class)
278     v = default_object_display_name(v)
279   end
280   v
281 end
columns_for(type, request)
[show source]
    # File lib/autoforme/model.rb
 99 def columns_for(type, request)
100   handle_proc(columns || framework.columns_for(model, type, request), type, request) || default_columns
101 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
350 def default_object_display_name(obj)
351   if obj.respond_to?(:forme_name)
352     obj.forme_name
353   elsif obj.respond_to?(:name)
354     obj.name
355   else
356     primary_key_value(obj)
357   end.to_s
358 end
destroy(obj)

Destroy the given object, deleting it from the database.

[show source]
    # File lib/autoforme/model.rb
284 def destroy(obj)
285   obj.destroy
286 end
display_name_for()
[show source]
    # File lib/autoforme/model.rb
213 def display_name_for
214   display_name || framework.display_name_for(model)
215 end
eager_for(type, request)
[show source]
    # File lib/autoforme/model.rb
173 def eager_for(type, request)
174   handle_proc(eager, type, request)
175 end
eager_graph_for(type, request)
[show source]
    # File lib/autoforme/model.rb
177 def eager_graph_for(type, request)
178   handle_proc(eager_graph, type, request)
179 end
edit_html_for(obj, column, type, request)
[show source]
    # File lib/autoforme/model.rb
165 def edit_html_for(obj, column, type, request)
166   handle_proc(edit_html || framework.edit_html_for(obj, column, type, request), obj, column, type, request)
167 end
filter_for()
[show source]
    # File lib/autoforme/model.rb
181 def filter_for
182   filter || framework.filter_for(model)
183 end
form_attributes_for(type, request)
[show source]
    # File lib/autoforme/model.rb
189 def form_attributes_for(type, request)
190   framework.form_attributes_for(model, type, request).merge(handle_proc(form_attributes, type, request) || {})
191 end
form_options_for(type, request)
[show source]
    # File lib/autoforme/model.rb
193 def form_options_for(type, request)
194   framework.form_options_for(model, type, request).merge(handle_proc(form_options, type, request) || {})
195 end
hook(type, request, obj)

Run given hooks with the related object and request.

[show source]
    # File lib/autoforme/model.rb
299 def hook(type, request, obj)
300   if type.to_s =~ /before/
301     if v = framework.send(type)
302       v.call(obj, request)
303     end
304     if v = send(type)
305       v.call(obj, request)
306     end
307   else
308     if v = send(type)
309       v.call(obj, request)
310     end
311     if v = framework.send(type)
312       v.call(obj, request)
313     end
314   end
315 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
95 def inline_mtm_assocs(request)
96   normalize_mtm_associations(handle_proc(inline_mtm_associations || framework.inline_mtm_associations_for(model, request), request))
97 end
limit_for(type, request)
[show source]
    # File lib/autoforme/model.rb
209 def limit_for(type, request)
210   handle_proc(per_page || framework.limit_for(model, type, request), type, request) || DEFAULT_LIMIT
211 end
model()

The underlying model class for the current model

[show source]
   # File lib/autoforme/model.rb
58 def model
59   if @model.is_a?(Class)
60     @model
61   elsif m = VALID_CONSTANT_NAME_REGEXP.match(@model)
62     Object.module_eval("::#{m[1]}", __FILE__, __LINE__)
63   else
64     raise Error, "invalid model for AutoForme::Model, not a class or valid constant name: #{@model.inspect}"
65   end
66 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
80 def mtm_association_select_options(request)
81   normalize_mtm_associations(handle_proc(mtm_associations || framework.mtm_associations_for(model, request), request))
82 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
319 def new(params, request)
320   obj = model.new
321   if params
322     columns_for(:new, request).each do |col|
323       if association?(col)
324         col = association_key(col)
325       end
326       if v = params[col.to_s]
327         obj.send("#{col}=", v)
328       end
329     end
330   end
331   obj
332 end
object_display_name(type, request, obj)

A human readable string representing the object.

[show source]
    # File lib/autoforme/model.rb
340 def object_display_name(type, request, obj)
341   apply_name_method(display_name_for, obj, type, request).to_s
342 end
order_for(type, request)
[show source]
    # File lib/autoforme/model.rb
169 def order_for(type, request)
170   handle_proc(order || framework.order_for(model, type, request), type, request)
171 end
page_header_for(type, request)
[show source]
    # File lib/autoforme/model.rb
201 def page_header_for(type, request)
202   handle_proc(page_header || framework.page_header_for(model, type, request), type, request)
203 end
pagination_strategy_for(type, request)
[show source]
    # File lib/autoforme/model.rb
103 def pagination_strategy_for(type, request)
104   handle_proc(pagination_strategy || framework.pagination_strategy_for(model, type, request), type, request) || DEFAULT_PAGINATION_STRATEGY
105 end
redirect_for()
[show source]
    # File lib/autoforme/model.rb
185 def redirect_for
186   redirect || framework.redirect_for(model)
187 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
335 def select_options(type, request)
336   all_rows_for(type, request).map{|obj| [object_display_name(type, request, obj), primary_key_value(obj)]}
337 end
show_html_for(obj, column, type, request)
[show source]
    # File lib/autoforme/model.rb
161 def show_html_for(obj, column, type, request)
162   handle_proc(show_html || framework.show_html_for(obj, column, type, request), obj, column, type, request)
163 end
supported_action?(type, request)

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

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

Whether an mtm_edit can be displayed for the given association

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

Whether an mtm_update can occur for the given association

[show source]
   # File lib/autoforme/model.rb
90 def supported_mtm_update?(assoc, request)
91   supported_mtm_edit?(assoc, request) || inline_mtm_assocs(request).map(&:to_s).include?(assoc) 
92 end
table_class_for(type, request)
[show source]
    # File lib/autoforme/model.rb
205 def table_class_for(type, request)
206   handle_proc(table_class || framework.table_class_for(model, type, request), type, request) || DEFAULT_TABLE_CLASS
207 end