Module ScaffoldingExtensions::MetaActiveRecord

  1. lib/scaffolding_extensions/model/active_record.rb

Class methods added to ActiveRecord::Base to allow it to work with Scaffolding Extensions.

Constants

SCAFFOLD_OPTIONS = ::ScaffoldingExtensions::MetaModel::SCAFFOLD_OPTIONS

Public instance methods

scaffold_add_associated_object (association, object, associated_object)

Add the associated object to the object’s association

[show source]
    # File lib/scaffolding_extensions/model/active_record.rb, line 21
21:   def scaffold_add_associated_object(association, object, associated_object)
22:     association_proxy = object.send(association)
23:     association_proxy << associated_object unless association_proxy.include?(associated_object)
24:   end
scaffold_all_associations ()

Array of all association reflections for this model

[show source]
    # File lib/scaffolding_extensions/model/active_record.rb, line 27
27:   def scaffold_all_associations
28:     reflect_on_all_associations
29:   end
scaffold_associated_class (association)

The class that this model is associated with via the association

[show source]
    # File lib/scaffolding_extensions/model/active_record.rb, line 32
32:   def scaffold_associated_class(association)
33:     scaffold_association(association).klass
34:   end
scaffold_association (association)

The association reflection for this association

[show source]
    # File lib/scaffolding_extensions/model/active_record.rb, line 37
37:   def scaffold_association(association)
38:     reflect_on_association(association)
39:   end
scaffold_association_type (association)

The type of association, either :new for :has_many (as you can create new objects associated with the current object), :edit for :has_and_belongs_to_many (since you can edit the list of associated objects), or :one for other associations. I’m not sure that :has_one is supported, as I don’t use it.

[show source]
    # File lib/scaffolding_extensions/model/active_record.rb, line 45
45:   def scaffold_association_type(association)
46:     case reflect_on_association(association).macro
47:       when :has_many
48:         :new
49:       when :has_and_belongs_to_many
50:         :edit
51:       else
52:         :one
53:     end
54:   end
scaffold_associations ()

List of symbols for associations to display on the scaffolded edit page. Defaults to all associations that aren’t :through or :polymorphic. Can be set with an instance variable.

[show source]
    # File lib/scaffolding_extensions/model/active_record.rb, line 58
58:   def scaffold_associations
59:     @scaffold_associations ||= scaffold_all_associations.reject{|r| r.options.include?(:through) || r.options.include?(:polymorphic)}.collect{|r| r.name}.sort_by{|name| name.to_s}
60:   end
scaffold_destroy (object)

Destroys the object

[show source]
    # File lib/scaffolding_extensions/model/active_record.rb, line 63
63:   def scaffold_destroy(object)
64:     object.destroy
65:   end
scaffold_error_raised ()

The error to raise, should match other errors raised by the underlying library.

[show source]
    # File lib/scaffolding_extensions/model/active_record.rb, line 68
68:   def scaffold_error_raised
69:     ::ActiveRecord::RecordNotFound
70:   end
scaffold_fields (action = :default)

Returns the list of fields to display on the scaffolded forms. Defaults to displaying all columns with the exception of primary key column, timestamp columns, count columns, and inheritance columns. Also includes belongs_to associations, replacing the foriegn keys with the association itself. Can be set with an instance variable.

[show source]
    # File lib/scaffolding_extensions/model/active_record.rb, line 76
76:   def scaffold_fields(action = :default)
77:     return @scaffold_fields if @scaffold_fields
78:     fields = columns.reject{|c| c.primary || c.name =~ /(\A(created|updated)_at|_count)\z/ || c.name == inheritance_column}.collect{|c| c.name}
79:     scaffold_all_associations.each do |reflection|
80:       next if reflection.macro != :belongs_to || reflection.options.include?(:polymorphic)
81:       fields.delete(reflection.primary_key_name)
82:       fields.push(reflection.name.to_s)
83:     end
84:     @scaffold_fields = fields.sort.collect{|f| f.to_sym}
85:   end
scaffold_foreign_key (reflection)

The foreign key for the given reflection

[show source]
    # File lib/scaffolding_extensions/model/active_record.rb, line 88
88:   def scaffold_foreign_key(reflection)
89:     reflection.primary_key_name
90:   end
scaffold_get_object (id)

Retrieve a single model object given an id

[show source]
    # File lib/scaffolding_extensions/model/active_record.rb, line 93
93:   def scaffold_get_object(id)
94:     find(id.to_i)
95:   end
scaffold_get_objects (options)

Retrieve multiple objects given a hash of options

[show source]
     # File lib/scaffolding_extensions/model/active_record.rb, line 98
 98:   def scaffold_get_objects(options)
 99:     options[:conditions] = scaffold_merge_conditions(options[:conditions])
100:     find(:all, options)
101:   end
scaffold_habtm_reflection_options (association)

Return the class, left foreign key, right foreign key, and join table for this habtm association

[show source]
     # File lib/scaffolding_extensions/model/active_record.rb, line 104
104:   def scaffold_habtm_reflection_options(association)
105:     reflection = reflect_on_association(association)
106:     [reflection.klass, reflection.primary_key_name, reflection.association_foreign_key, reflection.options[:join_table]]
107:   end
scaffold_new_associated_object_values (association, record)

Returns a hash of values to be used as url parameters on the link to create a new :has_many associated object. Defaults to setting the foreign key field to the record’s primary key, and the STI type to this model’s name, if :as is one of the association’s reflection’s options.

[show source]
     # File lib/scaffolding_extensions/model/active_record.rb, line 113
113:   def scaffold_new_associated_object_values(association, record)
114:     reflection = reflect_on_association(association)
115:     vals = {reflection.primary_key_name=>record.id}
116:     vals["#{reflection.options[:as]}_type"] = name if reflection.options.include?(:as)
117:     vals
118:   end
scaffold_primary_key ()

The primary key for the given table

[show source]
     # File lib/scaffolding_extensions/model/active_record.rb, line 121
121:   def scaffold_primary_key
122:     primary_key
123:   end
scaffold_save (action, object)

Saves the object.

[show source]
     # File lib/scaffolding_extensions/model/active_record.rb, line 126
126:   def scaffold_save(action, object)
127:     object.save
128:   end
scaffold_table_column_type (column)

The column type for the given table column, or nil if it isn’t a table column

[show source]
     # File lib/scaffolding_extensions/model/active_record.rb, line 131
131:   def scaffold_table_column_type(column)
132:     column = column.to_s
133:     column = columns_hash[column]
134:     column.type if column
135:   end
scaffold_table_name ()

The name of the underlying table

[show source]
     # File lib/scaffolding_extensions/model/active_record.rb, line 138
138:   def scaffold_table_name
139:     table_name
140:   end