Sha256: 6fe84be4ed01f133fb20daf8bb83d9164db2bbefd401f8ba2e7349a64295d653

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 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[column.to_s]
        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.0 lib/auto_set/set_column.rb