Class: ActiveRecord::CustomAttributes::CustomAttributeList

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

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ CustomAttributeList

Returns a new instance of CustomAttributeList.



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

def initialize(record)
  @record                = record
  @defined_attributes    = @record.class.defined_custom_attributes
  @defined_validations   = @record.class.defined_custom_validations
  @extra_attribute_types = @record.class.defined_custom_field_types
  define_attribute_methods
end

Instance Method Details

#add(type, label, value) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/active_record/custom_attributes/custom_attribute_list.rb', line 24

def add(type, label, value)
  type            = type.to_sym
  return if label.blank? and value.respond_to? :blank? and value.blank?
  internal_label  = convert_to_internal_label(type, label)
  attribute       = get_attribute(type, internal_label || label, true)
  attribute.value = value
end

#attributes_of_type(type) ⇒ Object



90
91
92
# File 'lib/active_record/custom_attributes/custom_attribute_list.rb', line 90

def attributes_of_type(type)
  loaded_attributes.select { |i| i.type == type.to_sym and !i.marked_for_destruction? }
end

#defined_attribute_typesObject



65
66
67
# File 'lib/active_record/custom_attributes/custom_attribute_list.rb', line 65

def defined_attribute_types
  (extra_attribute_types.keys + defined_attributes.keys).uniq
end

#defined_labels_for(type) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/active_record/custom_attributes/custom_attribute_list.rb', line 69

def defined_labels_for type
  results = []
  (defined_attributes[type.to_sym] || {}).each do |key, options|
    results << human_label_for(type, key)
  end
  results
end

#get_custom_validations_for(type, name) ⇒ Object



61
62
63
# File 'lib/active_record/custom_attributes/custom_attribute_list.rb', line 61

def get_custom_validations_for(type, name)
  [defined_validations[type], (defined_attributes[type][name] || {})[:validate_with]].compact
end

#rename_label_of(attribute, new_name) ⇒ Object



84
85
86
87
88
# File 'lib/active_record/custom_attributes/custom_attribute_list.rb', line 84

def rename_label_of attribute, new_name
  internal_label           = convert_to_internal_label(attribute.type, new_name)
  attribute.label          = new_name
  attribute.internal_label = internal_label
end

#saveObject



46
47
48
# File 'lib/active_record/custom_attributes/custom_attribute_list.rb', line 46

def save
  loaded_attributes.each(&:save)
end

#set_post_data(post_data) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/active_record/custom_attributes/custom_attribute_list.rb', line 11

def set_post_data(post_data)
  loaded_attributes.each(&:mark_for_destruction)
  post_data.each do |attribute_type, values|
    attribute_type = attribute_type.to_sym
    if supported_attribute_types.keys.include? attribute_type
      values.each do |key, attributes|
        removed = attributes["_destroy"].to_s == "true"
        add attribute_type, attributes["label"], attributes["value"] unless key == "%nr%" or removed
      end
    end
  end
end

#supported_attribute_typesObject



77
78
79
80
81
82
# File 'lib/active_record/custom_attributes/custom_attribute_list.rb', line 77

def supported_attribute_types
  return @supported_attribute_types if @supported_attribute_types
  standard_attribute_types   = {}
  ActiveRecord::CustomAttributes::CUSTOM_ATTRIBUTE_TYPES.each { |t| standard_attribute_types[t.to_sym] = t.to_sym }
  @supported_attribute_types = (standard_attribute_types.merge extra_attribute_types)
end

#update_cacheObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/active_record/custom_attributes/custom_attribute_list.rb', line 32

def update_cache
  defined_attributes.each do |type, attributes|
    attributes.each do |name, options|
      if cache_attribute = options[:on_model]
        if attribute = get_attribute(type, name)
          record.send("#{cache_attribute}=", attribute.value)
        else
          record.send("#{cache_attribute}=", nil)
        end
      end
    end
  end
end

#validate(errors) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/active_record/custom_attributes/custom_attribute_list.rb', line 50

def validate(errors)
  loaded_attributes.each do |a|
    a.validate
    unless a.valid?
      a.errors.each do |attribute, error_message|
        errors.add(:custom_attributes, error_message)
      end
    end
  end
end