Sha256: 37555034c79fd6914d86e6d1f31349176dc2a40bc5e2d12b03b6fce70218381b

Contents?: true

Size: 1.21 KB

Versions: 6

Compression:

Stored size: 1.21 KB

Contents

module Counter::Changable
  extend ActiveSupport::Concern

  included do
    def has_changed? attribute, from: Counter::Any, to: Counter::Any
      from = Counter::Any.instance if from == Counter::Any
      to = Counter::Any.instance if to == Counter::Any

      return false unless previous_changes.key?(attribute)

      old_value, new_value = previous_changes[attribute]

      # Return true on Counter::any changes
      return true if from.instance_of?(Counter::Any) && to.instance_of?(Counter::Any)

      from_condition = case from
      when Counter::Any then true
      when Proc then from.call(old_value)
      else
        from == old_value
      end

      to_condition = case to
      when Counter::Any then true
      when Proc then to.call(new_value)
      else
        to == new_value
      end

      # # Return false if nothing changed
      # return false if old_value == new_value

      # # Check if the value change from <something>
      # return new_value == to if from.instance_of?(Any)
      # # Check if the value change to <something>
      # return old_value == from if to.instance_of?(Any)

      # Check if the value change from <something> to <something>
      from_condition && to_condition
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
counterwise-0.1.5 app/models/concerns/counter/changable.rb
counterwise-0.1.4 app/models/concerns/counter/changable.rb
counterwise-0.1.3 app/models/concerns/counter/changable.rb
counterwise-0.1.2 app/models/concerns/counter/changable.rb
counterwise-0.1.1 app/models/concerns/counter/changable.rb
counterwise-0.1.0 app/models/concerns/counter/changable.rb