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, line 24
def self.for(framework, type, model_class, &block)
  model = AutoForme.model_class_for(type).new(model_class, framework)
  model.instance_exec(&block) if block
  model
end
new (model, framework)
[show source]
# File lib/autoforme/model.rb, line 45
def initialize(model, framework)
  @model = model
  @framework = framework
  @opts = {}
end

Public Instance methods

associated_model_class (assoc)

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

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

A human readable string for the associated object.

[show source]
# File lib/autoforme/model.rb, line 331
def associated_object_display_name(assoc, request, obj)
  apply_name_method(column_options_for(:mtm_edit, request, assoc)[:name_method], obj, :mtm_edit, request)
end
autocomplete_options_for (type, request)
[show source]
# File lib/autoforme/model.rb, line 225
def autocomplete_options_for(type, request)
  return unless AUTOCOMPLETE_TYPES.include?(type)
  framework_opts = framework.autocomplete_options_for(model, type, request)
  model_opts = handle_proc(autocomplete_options, type, request)
  if model_opts
    (framework_opts || {}).merge(model_opts)
  end
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, line 275
def before_action_hook(type, request)
  if v = framework.before_action
    v.call(type, request)
  end
  if v = before_action
    v.call(type, request)
  end
end
class_name ()

The name to display to the user for this model.

[show source]
# File lib/autoforme/model.rb, line 235
def class_name
  class_display_name || model.name
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, line 99
def column_options_for(type, request, column)
  framework_opts = case framework_opts = framework.column_options
  when Proc, Method
    framework_opts.call(model, column, type, request) || {}
  else
    extract_column_options(framework_opts, column, type, request)
  end

  model_opts = case model_opts = column_options
  when Proc, Method
    model_opts.call(column, type, request) || {}
  else
    extract_column_options(model_opts, column, type, request)
  end

  opts = framework_opts.merge(model_opts).dup

  if association?(column) && associated_model = associated_model_class(column)
    if associated_model.autocomplete_options_for(:association, request) && !opts[:as] && association_type(column) == :one
      opts[:type] = 'text'
      opts[:class] = 'autoforme_autocomplete'
      opts[:attr] = {'data-column'=>column, 'data-type'=>type}
      opts[:name] = form_param_name(column)
    else
      unless opts[:name_method]
        opts[:name_method] = lambda{|obj| associated_model.object_display_name(:association, request, obj)}
      end

      case type
      when :edit, :new, :search_form
        unless opts[:options] || opts[:dataset]
          opts[:dataset] = lambda{|ds| associated_model.apply_dataset_options(:association, request, ds)}
        end
      end
    end
  end

  case type
  when :show, :search_form
    opts[:required] = false unless opts.has_key?(:required)
    if type == :search_form && opts[:as] == :textarea
      opts.delete(:as)
    end
  end

  opts
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, line 250
def column_value(type, request, obj, column)
  v = obj.send(column)
  return if v.nil?
  if association?(column) 
    opts = column_options_for(type, request, column) 
    case nm = opts[:name_method]
    when Symbol, String
      v = v.send(nm)
    when nil
    else
      v = nm.call(v)
    end
  end
  if v.is_a?(base_class)
    v = default_object_display_name(v)
  end
  v
end
columns_for (type, request)
[show source]
# File lib/autoforme/model.rb, line 93
def columns_for(type, request)
  handle_proc(columns || framework.columns_for(model, type, request), type, request) || default_columns
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, line 336
def default_object_display_name(obj)
  if obj.respond_to?(:forme_name)
    obj.forme_name
  elsif obj.respond_to?(:name)
    obj.name
  else
    primary_key_value(obj)
  end.to_s
end
destroy (obj)

Destroy the given object, deleting it from the database.

[show source]
# File lib/autoforme/model.rb, line 270
def destroy(obj)
  obj.destroy
end
display_name_for ()
[show source]
# File lib/autoforme/model.rb, line 199
def display_name_for
  display_name || framework.display_name_for(model)
end
eager_for (type, request)
[show source]
# File lib/autoforme/model.rb, line 159
def eager_for(type, request)
  handle_proc(eager, type, request)
end
eager_graph_for (type, request)
[show source]
# File lib/autoforme/model.rb, line 163
def eager_graph_for(type, request)
  handle_proc(eager_graph, type, request)
end
edit_html_for (obj, column, type, request)
[show source]
# File lib/autoforme/model.rb, line 151
def edit_html_for(obj, column, type, request)
  handle_proc(edit_html || framework.edit_html_for(obj, column, type, request), obj, column, type, request)
end
filter_for ()
[show source]
# File lib/autoforme/model.rb, line 167
def filter_for
  filter || framework.filter_for(model)
end
form_attributes_for (type, request)
[show source]
# File lib/autoforme/model.rb, line 175
def form_attributes_for(type, request)
  framework.form_attributes_for(model, type, request).merge(handle_proc(form_attributes, type, request) || {})
end
form_options_for (type, request)
[show source]
# File lib/autoforme/model.rb, line 179
def form_options_for(type, request)
  framework.form_options_for(model, type, request).merge(handle_proc(form_options, type, request) || {})
end
hook (type, request, obj)

Run given hooks with the related object and request.

[show source]
# File lib/autoforme/model.rb, line 285
def hook(type, request, obj)
  if type.to_s =~ /before/
    if v = framework.send(type)
      v.call(obj, request)
    end
    if v = send(type)
      v.call(obj, request)
    end
  else
    if v = send(type)
      v.call(obj, request)
    end
    if v = framework.send(type)
      v.call(obj, request)
    end
  end
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, line 89
def inline_mtm_assocs(request)
  normalize_mtm_associations(handle_proc(inline_mtm_associations || framework.inline_mtm_associations_for(model, request), request))
end
limit_for (type, request)
[show source]
# File lib/autoforme/model.rb, line 195
def limit_for(type, request)
  handle_proc(per_page || framework.limit_for(model, type, request), type, request) || DEFAULT_LIMIT
end
model ()

The underlying model class for the current model

[show source]
# File lib/autoforme/model.rb, line 52
def model
  if @model.is_a?(Class)
    @model
  elsif m = VALID_CONSTANT_NAME_REGEXP.match(@model)
    Object.module_eval("::#{m[1]}", __FILE__, __LINE__)
  else
    raise Error, "invalid model for AutoForme::Model, not a class or valid constant name: #{@model.inspect}"
  end
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, line 74
def mtm_association_select_options(request)
  normalize_mtm_associations(handle_proc(mtm_associations || framework.mtm_associations_for(model, request), request))
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, line 305
def new(params, request)
  obj = model.new
  if params
    columns_for(:new, request).each do |col|
      if association?(col)
        col = association_key(col)
      end
      if v = params[col.to_s]
        obj.send("#{col}=", v)
      end
    end
  end
  obj
end
object_display_name (type, request, obj)

A human readable string representing the object.

[show source]
# File lib/autoforme/model.rb, line 326
def object_display_name(type, request, obj)
  apply_name_method(display_name_for, obj, type, request).to_s
end
order_for (type, request)
[show source]
# File lib/autoforme/model.rb, line 155
def order_for(type, request)
  handle_proc(order || framework.order_for(model, type, request), type, request)
end
page_header_for (type, request)
[show source]
# File lib/autoforme/model.rb, line 187
def page_header_for(type, request)
  handle_proc(page_header || framework.page_header_for(model, type, request), type, request)
end
redirect_for ()
[show source]
# File lib/autoforme/model.rb, line 171
def redirect_for
  redirect || framework.redirect_for(model)
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, line 321
def select_options(type, request)
  all_rows_for(type, request).map{|obj| [object_display_name(type, request, obj), primary_key_value(obj)]}
end
show_html_for (obj, column, type, request)
[show source]
# File lib/autoforme/model.rb, line 147
def show_html_for(obj, column, type, request)
  handle_proc(show_html || framework.show_html_for(obj, column, type, request), obj, column, type, request)
end
supported_action? (type, request)

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

[show source]
# File lib/autoforme/model.rb, line 63
def supported_action?(type, request)
  v = (handle_proc(supported_actions || framework.supported_actions_for(model, request), request) || DEFAULT_SUPPORTED_ACTIONS).include?(type)
  if v && type == :mtm_edit
    assocs = mtm_association_select_options(request)
    assocs && !assocs.empty?
  else
    v
  end
end
supported_mtm_edit? (assoc, request)

Whether an mtm_edit can be displayed for the given association

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

Whether an mtm_update can occur for the given association

[show source]
# File lib/autoforme/model.rb, line 84
def supported_mtm_update?(assoc, request)
  supported_mtm_edit?(assoc, request) || inline_mtm_assocs(request).map(&:to_s).include?(assoc) 
end
table_class_for (type, request)
[show source]
# File lib/autoforme/model.rb, line 191
def table_class_for(type, request)
  handle_proc(table_class || framework.table_class_for(model, type, request), type, request) || DEFAULT_TABLE_CLASS
end