Module: LargeTextField::Owner

Extended by:
ActiveSupport::Concern
Includes:
ActiveSupport::Callbacks
Defined in:
lib/large_text_field/owner.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#_clear_text_field_on_dupObject



79
80
81
82
83
# File 'lib/large_text_field/owner.rb', line 79

def _clear_text_field_on_dup
  if instance_variable_defined?(:@text_field_hash)
    remove_instance_variable(:@text_field_hash)
  end
end

#dupObject



16
17
18
19
20
21
22
23
24
# File 'lib/large_text_field/owner.rb', line 16

def dup
  result = super

  result._clear_text_field_on_dup

  large_text_field_options.keys.each { |k| result.set_text_field(k, get_text_field(k)) }

  result
end

#get_text_field(field_name) ⇒ Object



40
41
42
# File 'lib/large_text_field/owner.rb', line 40

def get_text_field(field_name)
  text_field_hash[field_name.to_s]._?.value || ''
end

#reloadObject



26
27
28
29
30
# File 'lib/large_text_field/owner.rb', line 26

def reload
  super
  @text_field_hash = nil
  self
end

#set_text_field(original_field_name, original_value) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/large_text_field/owner.rb', line 44

def set_text_field(original_field_name, original_value)
  !original_value.nil? or raise "LargeTextField value cannot be set value to nil."

  field_name = original_field_name.to_s
  value = original_value.is_a?(File) ? original_value.read : original_value.to_s
  if (field = text_field_hash[field_name])
    field.value = value
  else
    text_field_hash[field_name] = LargeTextField::NamedTextValue.new(owner: self, field_name: field_name, value: value)
  end
end

#text_field_changed(field_name) ⇒ Object



56
57
58
# File 'lib/large_text_field/owner.rb', line 56

def text_field_changed(field_name)
  text_field_hash_loaded? && @text_field_hash[field_name]._?.changes._?.any?
end

#text_field_hashObject



32
33
34
# File 'lib/large_text_field/owner.rb', line 32

def text_field_hash
  @text_field_hash ||= large_text_fields.build_hash { |text_field| [text_field.field_name, text_field] }
end

#text_field_hash_loaded?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/large_text_field/owner.rb', line 36

def text_field_hash_loaded?
  defined?(@text_field_hash) && @text_field_hash.present?
end

#validate_large_text_fieldsObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/large_text_field/owner.rb', line 60

def validate_large_text_fields
  if text_field_hash_loaded?
    large_text_field_options.each do |k, options|
      value = text_field_hash[k]._?.value
      conjugation = options[:singularize_errors] ? "is" : "are"
      maximum = options[:maximum] || MAX_LENGTH
      errors.add k, "#{conjugation} too long (maximum is #{self.class.formatted_integer_value(maximum)} characters)" if value.present? && value.size > maximum
    end
  end
end

#write_large_text_field_changesObject



71
72
73
74
75
76
77
# File 'lib/large_text_field/owner.rb', line 71

def write_large_text_field_changes
  run_callbacks(:large_text_field_save)

  @text_field_hash = text_field_hash.compact.select { |_k, v| v.value.presence }
  self.large_text_fields = text_field_hash.values.compact
  true
end