Wraps a specific model class
Methods
Public Class
Public Instance
- associated_model_class
- associated_object_display_name
- association_links_for
- autocomplete_options_for
- before_action_hook
- class_name
- column_options_for
- column_search_filter_for
- column_value
- columns_for
- default_object_display_name
- destroy
- display_name_for
- eager_for
- eager_graph_for
- edit_html_for
- filter_for
- form_attributes_for
- form_options_for
- framework
- hook
- inline_mtm_assocs
- lazy_load_association_links?
- limit_for
- link
- model
- mtm_association_select_options
- new
- object_display_name
- opts
- order_for
- page_footer_for
- page_header_for
- pagination_strategy_for
- redirect_for
- select_options
- show_html_for
- supported_action?
- supported_mtm_edit?
- supported_mtm_update?
- table_class_for
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 |
opts | [R] |
The options for the given model. |
Public Class methods
Create a new instance for the given model type and underlying model class tied to the given framework.
# 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
# File lib/autoforme/model.rb 51 def initialize(model, framework) 52 @model = model 53 @framework = framework 54 @opts = {} 55 end
Public Instance methods
The AutoForme::Model
instance associated to the given association.
# File lib/autoforme/model.rb 259 def associated_model_class(assoc) 260 framework.model_class(associated_class(assoc)) 261 end
A human readable string for the associated object.
# 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
# File lib/autoforme/model.rb 217 def association_links_for(type, request) 218 case v = handle_proc(association_links || framework.association_links_for(model, type, request), type, request) 219 when nil 220 [] 221 when Array 222 v 223 when :all 224 association_names 225 when :all_except_mtm 226 association_names - mtm_association_names 227 else 228 [v] 229 end 230 end
# 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
Run framework and model before_action hooks with type symbol and request.
# 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
The name to display to the user for this model.
# File lib/autoforme/model.rb 249 def class_name 250 class_display_name || model.name 251 end
The options to use for the given column and request. Instead of the model options overriding the framework options, they are merged together.
# 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
# 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
The column value to display for the given object and column.
# 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
# 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
A fallback for the display name for the object if none is configured.
# 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 the given object, deleting it from the database.
# File lib/autoforme/model.rb 284 def destroy(obj) 285 obj.destroy 286 end
# File lib/autoforme/model.rb 213 def display_name_for 214 display_name || framework.display_name_for(model) 215 end
# File lib/autoforme/model.rb 173 def eager_for(type, request) 174 handle_proc(eager, type, request) 175 end
# File lib/autoforme/model.rb 177 def eager_graph_for(type, request) 178 handle_proc(eager_graph, type, request) 179 end
# 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
# File lib/autoforme/model.rb 181 def filter_for 182 filter || framework.filter_for(model) 183 end
# 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
# 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
Run given hooks with the related object and request.
# 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
An array of many to many association symbols to handle inline on the edit forms.
# 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
Whether to lazy load association links for this model.
# File lib/autoforme/model.rb 233 def lazy_load_association_links?(type, request) 234 v = handle_proc(lazy_load_association_links, type, request) 235 v = framework.lazy_load_association_links?(model, type, request) if v.nil? 236 v || false 237 end
# 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
The name to use in links for this model. Also affects where this model is mounted at.
# File lib/autoforme/model.rb 254 def link 255 link_name || class_name 256 end
The underlying model class for the current model
# 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
An array of many to many association symbols to handle via a separate mtm_edit page.
# 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
Create a new instance of the underlying model, setting defaults based on the params given.
# 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
A human readable string representing the object.
# 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
# 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
# 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
# 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
# File lib/autoforme/model.rb 185 def redirect_for 186 redirect || framework.redirect_for(model) 187 end
An array of pairs for the select options to return for the given type.
# 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
# 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
Whether the given type of action is supported for this model.
# 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
Whether an mtm_edit can be displayed for the given association
# 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
Whether an mtm_update can occur for the given association
# 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
# 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