Class: AssignableValues::ActiveRecord::Restriction::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/assignable_values/active_record/restriction/base.rb

Direct Known Subclasses

BelongsToAssociation, ScalarAttribute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, property, options, &values) ⇒ Base

Returns a new instance of Base.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/assignable_values/active_record/restriction/base.rb', line 20

def initialize(model, property, options, &values)
  @model = model
  @property = property
  @options = options
  @values = values
  validate_supported_options!
  ensure_values_given
  setup_default
  define_assignable_values_method
  setup_validation
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



6
7
8
# File 'lib/assignable_values/active_record/restriction/base.rb', line 6

def default
  @default
end

#modelObject (readonly)

Returns the value of attribute model.



6
7
8
# File 'lib/assignable_values/active_record/restriction/base.rb', line 6

def model
  @model
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/assignable_values/active_record/restriction/base.rb', line 6

def options
  @options
end

#propertyObject (readonly)

Returns the value of attribute property.



6
7
8
# File 'lib/assignable_values/active_record/restriction/base.rb', line 6

def property
  @property
end

#secondary_defaultObject (readonly)

Returns the value of attribute secondary_default.



6
7
8
# File 'lib/assignable_values/active_record/restriction/base.rb', line 6

def secondary_default
  @secondary_default
end

#valuesObject (readonly)

Returns the value of attribute values.



6
7
8
# File 'lib/assignable_values/active_record/restriction/base.rb', line 6

def values
  @values
end

Instance Method Details

#assignable_values(record, options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/assignable_values/active_record/restriction/base.rb', line 63

def assignable_values(record, options = {})
  additional_assignable_values = []
  current_values = assignable_values_from_record_or_delegate(record)

  if options.fetch(:include_old_value, true) && has_previously_saved_value?(record)
    old_value = previously_saved_value(record)
    if @options[:multiple]
      if old_value.is_a?(Array)
        additional_assignable_values = old_value
      end
    elsif !old_value.blank? && !current_values.include?(old_value)
      additional_assignable_values << old_value
    end
  end

  if options[:decorate]
    current_values = decorate_values(current_values, record.class)
    additional_assignable_values = decorate_values(additional_assignable_values, record.class)
  end

  if additional_assignable_values.present?
    # will not keep current_values scoped
    additional_assignable_values | current_values
  else
    current_values
  end
end

#set_default(record) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/assignable_values/active_record/restriction/base.rb', line 45

def set_default(record)
  if record.new_record? && record.send(property).nil?
    default_value = evaluate_default(record, default)
    begin
      if secondary_default? && !assignable_value?(record, default_value)
        secondary_default_value = evaluate_default(record, secondary_default)
        if assignable_value?(record, secondary_default_value)
          default_value = secondary_default_value
        end
      end
    rescue AssignableValues::DelegateUnavailable
      # skip secondary defaults if querying assignable values from a nil delegate
    end
    record.send("#{property}=", default_value)
  end
  true
end

#validate_record(record) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/assignable_values/active_record/restriction/base.rb', line 32

def validate_record(record)
  value = current_value(record)
  unless allow_blank?(record) && value.blank?
    begin
      unless assignable_value?(record, value)
        record.errors.add(error_property, not_included_error_message)
      end
    rescue DelegateUnavailable
      # if the delegate is unavailable, the validation is skipped
    end
  end
end