Sha256: c786d15b916626386371d61b06133a3768a3ac5da026aa40af98c0c675ba5bce

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

module AutoSet
  class SetColumn
    def initialize(column, parents, options = {})
      @column = column

      if parents.is_a? Hash
        @options = parents.reverse_merge from: :code
      else
        @options = options || {}
        @parents = parents
        @parents = @parents.is_a?(Array) ? @parents : [ @parents ]
      end
    end

    def before_save(record)
      @record = record
      if @parents.present?
        direct
      else
        from_column
      end
    end

    def before_create(record)
      before_save record
    end

    def before_validation(record)
      before_save record
    end

    private

    def direct
      if @record.send(@column).blank?
        @record.send "#{@column}=", parent_object.send(@column)
      end
    end

    def from_column
      if @record.send("#{@column}_#{@options[:from]}_changed?")
        from_column_changed
      elsif @record.send("#{@column}_id_changed?")
        from_column_id_changed
      end
    end

    def parent_object
      parent = @record
      @parents.each do |parent_name|
        parent = parent.send(parent_name) if parent.respond_to? parent_name
      end

      parent
    end

    def parent_from_column(column, from)
      if @record.send("#{column}_#{from}").present?
        reflection = @record.class.reflections.with_indifferent_access[column]
        reflection.klass.where(from => @record.send("#{column}_#{from}")).first if reflection.present?
      end
    end

    def from_column_changed
      @record.send "#{@column}=", parent_from_column(@column, @options[:from])
    end

    def from_column_id_changed
      value = @record.send(@column).code if @record.send(@column).present?
      @record.send "#{@column}_#{@options[:from]}=", value
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
auto_set-1.2.1 lib/auto_set/set_column.rb