Class: ActiveRecord::CustomAttributes::CustomAttribute

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/custom_attributes/custom_attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item_list, main_model, attribute_model) ⇒ CustomAttribute

Returns a new instance of CustomAttribute.



3
4
5
6
7
8
9
10
# File 'lib/active_record/custom_attributes/custom_attribute.rb', line 3

def initialize(item_list, main_model, attribute_model)
  @main_model             = main_model
  @item_list              = item_list
  @attribute_model        = attribute_model
  @marked_for_destruction = false
  @errors                 = ActiveModel::Errors.new(self)
  load if @attribute_model
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



60
61
62
# File 'lib/active_record/custom_attributes/custom_attribute.rb', line 60

def errors
  @errors
end

Instance Method Details

#mark_for_destructionObject



26
27
28
# File 'lib/active_record/custom_attributes/custom_attribute.rb', line 26

def mark_for_destruction
  @marked_for_destruction = true
end

#marked_for_destruction?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/active_record/custom_attributes/custom_attribute.rb', line 30

def marked_for_destruction?
  @marked_for_destruction
end

#rename_to(new_name) ⇒ Object



22
23
24
# File 'lib/active_record/custom_attributes/custom_attribute.rb', line 22

def rename_to new_name
  item_list.rename_label_of self, new_name
end

#saveObject



54
55
56
57
58
# File 'lib/active_record/custom_attributes/custom_attribute.rb', line 54

def save
  attribute_model.destroy and return if marked_for_destruction?
  assign_model_value
  attribute_model.save
end

#valid?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/active_record/custom_attributes/custom_attribute.rb', line 50

def valid?
  errors.empty?
end

#validateObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/active_record/custom_attributes/custom_attribute.rb', line 34

def validate
  errors.clear
  return if marked_for_destruction?
  assign_model_value
  unless attribute_model.valid?
    attribute_model.errors.each do |attribute, message|
      errors.add :value, message
    end
  end
  validations = item_list.get_custom_validations_for type, internal_label
  validations.each do |validator|
    main_model.send(validator, self) if validator.is_a? Symbol
    validator.call(self) if validator.is_a? Proc
  end
end