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



83
84
85
86
87
# File 'lib/large_text_field/owner.rb', line 83

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

#dupObject



20
21
22
23
24
25
26
27
28
# File 'lib/large_text_field/owner.rb', line 20

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



44
45
46
# File 'lib/large_text_field/owner.rb', line 44

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

#reload(options = nil) ⇒ Object



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

def reload(options = nil)
  super(options)
  @text_field_hash = nil
  self
end

#set_text_field(original_field_name, original_value) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/large_text_field/owner.rb', line 48

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



60
61
62
# File 'lib/large_text_field/owner.rb', line 60

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

#text_field_hashObject



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

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)


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

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

#validate_large_text_fieldsObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/large_text_field/owner.rb', line 64

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



75
76
77
78
79
80
81
# File 'lib/large_text_field/owner.rb', line 75

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