Sha256: ee5110fe9e9121d9f6413ee768f9086f3817e0a366cb711fd0dbe0cce8f31cd9

Contents?: true

Size: 1.64 KB

Versions: 6

Compression:

Stored size: 1.64 KB

Contents

# monkey patch for https://github.com/mixonic/ranked-model/issues/41
module RankedModel
  class Ranker
    class Mapper
      def finder
        @finder ||= begin
          _finder = instance_class
          columns = [instance_class.arel_table[:id], instance_class.arel_table[ranker.column]]
          if ranker.scope
            _finder = _finder.send ranker.scope
          end
          case ranker.with_same
            when Symbol
               columns << ranker.with_same
              _finder = _finder.where \
                instance_class.arel_table[ranker.with_same].eq(instance.attributes["#{ranker.with_same}"])
            when Array
              columns.push *ranker.with_same
              _finder = _finder.where(
                ranker.with_same[1..-1].inject(
                  instance_class.arel_table[ranker.with_same.first].eq(
                    instance.attributes["#{ranker.with_same.first}"]
                  )
                ) {|scoper, attr|
                  scoper.and(
                    instance_class.arel_table[attr].eq(
                      instance.attributes["#{attr}"]
                    )
                  )
                }
              )
          end
          if !new_record?
            _finder = _finder.where \
              instance_class.arel_table[instance_class.primary_key].not_eq(instance.id)
          end
          # _finder.order(instance_class.arel_table[ranker.column].asc).select([instance_class.arel_table[instance_class.primary_key], instance_class.arel_table[ranker.column]])
          _finder.order(instance_class.arel_table[ranker.column].asc).select(columns)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
cardboard_cms-0.3.1 config/initializers/ranker_mapper_finder_bugfix.rb
cardboard_cms-0.2.2 config/initializers/ranker_mapper_finder_bugfix.rb
cardboard_cms-0.2.1 config/initializers/ranker_mapper_finder_bugfix.rb
cardboard_cms-0.1.8 config/initializers/ranker_mapper_finder_bugfix.rb
cardboard_cms-0.1.6 config/initializers/ranker_mapper_finder_bugfix.rb
cardboard_cms-0.1.4 config/initializers/ranker_mapper_finder_bugfix.rb