Class: ActiveForm::Model::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/model/loader.rb

Direct Known Subclasses

AutoDefinition

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDefinition

Returns a new instance of Definition.



80
81
82
# File 'lib/rails/model/loader.rb', line 80

def initialize
  @now = Time.now
end

Class Method Details

.belongs_to_column(assoc, column, options = {}) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/rails/model/loader.rb', line 138

def belongs_to_column(assoc, column, options = {})
  if assoc.klass.respond_to?(:dropdown_text_attr)
    ActiveForm::Element::build(:select_from_model, column.name, options.merge(:type_cast => :integer, :model => assoc.klass.to_s, :to_dropdown => true))
  else
    integer_column(column, options)
  end
end

.boolean_column(column, options = {}) ⇒ Object



173
174
175
# File 'lib/rails/model/loader.rb', line 173

def boolean_column(column, options = {})
  ActiveForm::Element::build(:text, column.name, options.merge(:type_cast => :boolean))
end

.date_column(column, options = {}) ⇒ Object



164
165
166
# File 'lib/rails/model/loader.rb', line 164

def date_column(column, options = {})
  ActiveForm::Element::build(:select_date, column.name, options.merge(:type_cast => :date))
end

.datetime_column(column, options = {}) ⇒ Object Also known as: timestamp_column



168
169
170
# File 'lib/rails/model/loader.rb', line 168

def datetime_column(column, options = {})
  ActiveForm::Element::build(:select_datetime, column.name, options.merge(:type_cast => :time))
end

.integer_column(column, options = {}) ⇒ Object Also known as: float_column



159
160
161
# File 'lib/rails/model/loader.rb', line 159

def integer_column(column, options = {})
  ActiveForm::Element::build(:text, column.name, options.merge(:type_cast => :integer))
end

.primary_key_column(column, options = {}) ⇒ Object



146
147
148
# File 'lib/rails/model/loader.rb', line 146

def primary_key_column(column, options = {})
  ActiveForm::Element::build(:hidden, column.name, options.merge(:type_cast => :integer))
end

.string_column(column, options = {}) ⇒ Object



150
151
152
153
# File 'lib/rails/model/loader.rb', line 150

def string_column(column, options = {})
  type = column.name =~ /password/i ? :password : :text
  ActiveForm::Element::build(type, column.name, options.merge(:type_cast => :string))
end

.text_column(column, options = {}) ⇒ Object



155
156
157
# File 'lib/rails/model/loader.rb', line 155

def text_column(column, options = {})
  ActiveForm::Element::build(:textarea, column.name, options.merge(:type_cast => :text))
end

Instance Method Details

#assign_validation(form, instance) ⇒ Object



94
95
96
# File 'lib/rails/model/loader.rb', line 94

def assign_validation(form, instance)
  # TODO reflect_on_validations here
end

#association_column_to_element(assoc, column, options = {}) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/rails/model/loader.rb', line 119

def association_column_to_element(assoc, column, options = {})
  options[:default] = column.default unless column.default.blank?
  
  element = self.class.respond_to?("#{assoc.macro}_column") ? self.class.send("#{assoc.macro}_column", assoc, column, options) : self.class.integer_column(column, options)
  element.label = column.human_name
  element
end

#associations_lookup(instance) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/rails/model/loader.rb', line 127

def associations_lookup(instance)
  instance.class.reflect_on_all_associations.inject({}) do |lookup, assoc|
    if assoc.macro == :belongs_to
      lookup[assoc.primary_key_name] = assoc
    end
    lookup
  end
end

#build(name, instance, *args, &block) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/rails/model/loader.rb', line 84

def build(name, instance, *args, &block)
  form = ActiveForm::build(name, *args, &block)        
  pk_column = instance.column_for_attribute(instance.class.primary_key)
  form.insert_element_at_top(self.class.primary_key_column(pk_column)) if !pk_column.nil? && form[instance.class.primary_key.to_sym].nil?
  instance.class.columns.each { |column| (elem = form[column.name]) ? elem.type_cast = column.type : nil }
  form = instance.new_record? ? prepare_new_record(form, instance) : prepare_record(form, instance)
  assign_validation(form, instance)
  form
end

#column_to_element(column, options = {}) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/rails/model/loader.rb', line 111

def column_to_element(column, options = {})
  options[:default] = column.default unless column.default.blank?
  
  element = self.class.respond_to?("#{column.type}_column") ? self.class.send("#{column.type}_column", column, options) : self.class.string_column(column, options)
  element.label = column.human_name
  element
end

#prepare_new_record(form, instance) ⇒ Object



98
99
100
101
102
103
# File 'lib/rails/model/loader.rb', line 98

def prepare_new_record(form, instance)
  prepare_record(form, instance)
  form.get_elements_of_type(:select_date, :select_time, :select_datetime).each { |elem| elem.value = @now if elem.blank? }
  instance.class.columns.each { |column| ((elem = form[column.name]) && !column.default.blank?) ? elem.type_cast = column.default : nil }
  form
end

#prepare_record(form, instance) ⇒ Object



105
106
107
108
109
# File 'lib/rails/model/loader.rb', line 105

def prepare_record(form, instance)
  form.update_values(ActiveForm::Values.new(instance.attributes))
  form.model_instance = instance
  form
end