Sha256: ae7076f4492cbb70c967ab4e883d7029c7a241cdefd0f4d3bdf43e71a6ed6d61

Contents?: true

Size: 683 Bytes

Versions: 4

Compression:

Stored size: 683 Bytes

Contents

module DynamicFields
  class Changes::CreateOrUpdate
    attr_reader :record, :value, :field_name, :field_type, :attribute

    def initialize(value, field_name, field_type, record)
      @value, @field_name, @field_type, @record = value, field_name, field_type, record
      @attribute = find_or_build_attribute
    end

    def save
      attribute.value = value
      record.public_send("#{field_name}_attribute=", attribute)
    end

    private

    def find_or_build_attribute
      record.dynamic_fields_attributes.find_or_initialize_by(
        name: field_name,
        type: DynamicFields::Attribute.attribute_class_for_attribute_type(field_type)
      )
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dynamic_fields_rails-0.1.3 lib/dynamic_fields/changes/create_or_update.rb
dynamic_fields_rails-0.1.2 lib/dynamic_fields/changes/create_or_update.rb
dynamic_fields_rails-0.1.1 lib/dynamic_fields/changes/create_or_update.rb
dynamic_fields_rails-0.1.0 lib/dynamic_fields/changes/create_or_update.rb