Module: ScaffoldingExtensions::MetaActiveRecord

Included in:
ActiveRecord::Base
Defined in:
lib/scaffolding_extensions/model/active_record.rb

Overview

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

Constant Summary collapse

SCAFFOLD_OPTIONS =
::ScaffoldingExtensions::MetaModel::SCAFFOLD_OPTIONS

Instance Method Summary collapse

Instance Method Details

#scaffold_add_associated_object(association, object, associated_object) ⇒ Object

Add the associated object to the object’s association



21
22
23
24
# File 'lib/scaffolding_extensions/model/active_record.rb', line 21

def scaffold_add_associated_object(association, object, associated_object)
  association_proxy = object.send(association)
  association_proxy << associated_object unless association_proxy.include?(associated_object)
end

#scaffold_all_associationsObject

Array of all association reflections for this model



27
28
29
# File 'lib/scaffolding_extensions/model/active_record.rb', line 27

def scaffold_all_associations
  reflect_on_all_associations
end

#scaffold_associated_class(association) ⇒ Object

The class that this model is associated with via the association



32
33
34
# File 'lib/scaffolding_extensions/model/active_record.rb', line 32

def scaffold_associated_class(association)
  scaffold_association(association).klass
end

#scaffold_association(association) ⇒ Object

The association reflection for this association



37
38
39
# File 'lib/scaffolding_extensions/model/active_record.rb', line 37

def scaffold_association(association)
  reflect_on_association(association)
end

#scaffold_association_type(association) ⇒ Object

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.



45
46
47
48
49
50
51
52
53
54
# File 'lib/scaffolding_extensions/model/active_record.rb', line 45

def scaffold_association_type(association)
  case reflect_on_association(association).macro
    when :has_many
      :new
    when :has_and_belongs_to_many
      :edit
    else
      :one
  end
end

#scaffold_associationsObject

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.



58
59
60
# File 'lib/scaffolding_extensions/model/active_record.rb', line 58

def scaffold_associations
  @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}
end

#scaffold_destroy(object) ⇒ Object

Destroys the object



63
64
65
# File 'lib/scaffolding_extensions/model/active_record.rb', line 63

def scaffold_destroy(object)
  object.destroy
end

#scaffold_error_raisedObject

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



68
69
70
# File 'lib/scaffolding_extensions/model/active_record.rb', line 68

def scaffold_error_raised
  ::ActiveRecord::RecordNotFound
end

#scaffold_fields(action = :default) ⇒ Object

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.



76
77
78
79
80
81
82
83
84
85
# File 'lib/scaffolding_extensions/model/active_record.rb', line 76

def scaffold_fields(action = :default)
  return @scaffold_fields if @scaffold_fields
  fields = columns.reject{|c| c.primary || c.name =~ /(\A(created|updated)_at|_count)\z/ || c.name == inheritance_column}.collect{|c| c.name}
  scaffold_all_associations.each do |reflection|
    next if reflection.macro != :belongs_to || reflection.options.include?(:polymorphic)
    fields.delete(reflection.primary_key_name)
    fields.push(reflection.name.to_s)
  end
  @scaffold_fields = fields.sort.collect{|f| f.to_sym}
end

#scaffold_foreign_key(reflection) ⇒ Object

The foreign key for the given reflection



88
89
90
# File 'lib/scaffolding_extensions/model/active_record.rb', line 88

def scaffold_foreign_key(reflection)
  reflection.primary_key_name
end

#scaffold_get_object(id) ⇒ Object

Retrieve a single model object given an id



93
94
95
# File 'lib/scaffolding_extensions/model/active_record.rb', line 93

def scaffold_get_object(id)
  find(id.to_i)
end

#scaffold_get_objects(options) ⇒ Object

Retrieve multiple objects given a hash of options



98
99
100
101
# File 'lib/scaffolding_extensions/model/active_record.rb', line 98

def scaffold_get_objects(options)
  options[:conditions] = scaffold_merge_conditions(options[:conditions])
  find(:all, options)
end

#scaffold_habtm_reflection_options(association) ⇒ Object

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



104
105
106
107
# File 'lib/scaffolding_extensions/model/active_record.rb', line 104

def scaffold_habtm_reflection_options(association)
  reflection = reflect_on_association(association)
  [reflection.klass, reflection.primary_key_name, reflection.association_foreign_key, reflection.options[:join_table]]
end

#scaffold_new_associated_object_values(association, record) ⇒ Object

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.



113
114
115
116
117
118
# File 'lib/scaffolding_extensions/model/active_record.rb', line 113

def scaffold_new_associated_object_values(association, record)
  reflection = reflect_on_association(association)
  vals = {reflection.primary_key_name=>record.id}
  vals["#{reflection.options[:as]}_type"] = name if reflection.options.include?(:as)
  vals
end

#scaffold_primary_keyObject

The primary key for the given table



121
122
123
# File 'lib/scaffolding_extensions/model/active_record.rb', line 121

def scaffold_primary_key
  primary_key
end

#scaffold_save(action, object) ⇒ Object

Saves the object.



126
127
128
# File 'lib/scaffolding_extensions/model/active_record.rb', line 126

def scaffold_save(action, object)
  object.save
end

#scaffold_table_column_type(column) ⇒ Object

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



131
132
133
134
135
# File 'lib/scaffolding_extensions/model/active_record.rb', line 131

def scaffold_table_column_type(column)
  column = column.to_s
  column = columns_hash[column]
  column.type if column
end

#scaffold_table_nameObject

The name of the underlying table



138
139
140
# File 'lib/scaffolding_extensions/model/active_record.rb', line 138

def scaffold_table_name
  table_name
end