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
- #_clear_text_field_on_dup ⇒ Object
- #dup ⇒ Object
- #get_text_field(field_name) ⇒ Object
- #reload(options = nil) ⇒ Object
- #set_text_field(original_field_name, original_value) ⇒ Object
- #text_field_changed(field_name) ⇒ Object
- #text_field_hash ⇒ Object
- #text_field_hash_loaded? ⇒ Boolean
-
#validate_large_text_fields ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity.
-
#write_large_text_field_changes ⇒ Object
rubocop:enable Metrics/CyclomaticComplexity.
Instance Method Details
#_clear_text_field_on_dup ⇒ Object
113 114 115 116 117 |
# File 'lib/large_text_field/owner.rb', line 113 def _clear_text_field_on_dup if instance_variable_defined?(:@text_field_hash) remove_instance_variable(:@text_field_hash) end end |
#dup ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/large_text_field/owner.rb', line 32 def dup result = super result._clear_text_field_on_dup .each_key { |key| result.set_text_field(key, get_text_field(key)) } result end |
#get_text_field(field_name) ⇒ Object
63 64 65 |
# File 'lib/large_text_field/owner.rb', line 63 def get_text_field(field_name) text_field_hash[field_name.to_s]&.value || '' end |
#reload(options = nil) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/large_text_field/owner.rb', line 42 def reload( = nil) super() @text_field_hash = nil self end |
#set_text_field(original_field_name, original_value) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/large_text_field/owner.rb', line 67 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] = large_text_field_class.new(owner: self, field_name:, value:) end end |
#text_field_changed(field_name) ⇒ Object
79 80 81 |
# File 'lib/large_text_field/owner.rb', line 79 def text_field_changed(field_name) text_field_hash_loaded? && @text_field_hash[field_name]&.changes&.any? end |
#text_field_hash ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/large_text_field/owner.rb', line 49 def text_field_hash unless @text_field_hash @text_field_hash = large_text_fields.build_hash { |text_field| [text_field.field_name, text_field] } if large_text_field_deprecated_class_name deprecated_large_text_fields.each { |text_field| @text_field_hash[text_field.field_name] ||= text_field } end end @text_field_hash end |
#text_field_hash_loaded? ⇒ Boolean
59 60 61 |
# File 'lib/large_text_field/owner.rb', line 59 def text_field_hash_loaded? defined?(@text_field_hash) && @text_field_hash.present? end |
#validate_large_text_fields ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/large_text_field/owner.rb', line 84 def validate_large_text_fields if text_field_hash_loaded? .each do |key, | value = text_field_hash[key]&.value conjugation = [:singularize_errors] ? "is" : "are" maximum = [:maximum] || MAX_LENGTH if value.present? && value.size > maximum errors.add( key, "#{conjugation} too long (maximum is #{self.class.formatted_integer_value(maximum)} characters)" ) end end end end |
#write_large_text_field_changes ⇒ Object
rubocop:enable Metrics/CyclomaticComplexity
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/large_text_field/owner.rb', line 102 def write_large_text_field_changes run_callbacks(:large_text_field_save) @text_field_hash = text_field_hash .compact .select { |_key, value| value.value.presence } .transform_values { |value| value.is_a?(large_text_field_class) ? value : large_text_field_class.new(owner: self, field_name: value.field_name, value: value.value) } self.large_text_fields = text_field_hash.values.compact true end |