Sha256: eb23bcc20c4d9142888659b03bb2f074fc668cbd041dc672f6f2080be73245fa

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

module ActiveRecord
  module Filter
    extend ActiveSupport::Concern

    module ClassMethods
      def filterize(attributes = modal_attributes_with_options, options = {})
        if attributes.is_a?(Hash)
          options = attributes
          attributes = accessible_attributes
        end

        attributes = Array(attributes)

        class_eval(<<-eoruby, __FILE__, __LINE__ + 1)
          def self.filter(params)
            Filters::Filter.new(self, #{attributes}, #{options}).call(params)
          end
        eoruby
      end

      def orderize(*attributes)
        options = attributes.extract_options!

        if options.any?
          include_table = options.fetch(:on)
          table_name = include_table.to_s.classify.constantize.table_name
          attributes = "#{table_name}.#{attributes.first}" if include_table.present?
        else
          attributes = [:name] if attributes.empty?

          attributes.map! do |attribute|
            # Converte symbols para Arel::Nodes::Ordering
            attribute = arel_table[attribute].asc if attribute.is_a?(Symbol)

            if attribute.is_a?(Arel::Attributes::Attribute)
              attribute = attribute.asc
            end

            if attribute.class < Arel::Nodes::Ordering
              # Converte Arel::Nodes::Ordering para string com prefixo
              # da tabela e quoted
              arel_table.engine.connection.visitor.accept attribute
            else
              attribute
            end
          end

          attributes = attributes.join(', ')
        end

        class_eval(<<-eoruby, __FILE__, __LINE__ + 1)
          def self.ordered
            query = order('#{attributes}')

            #{"query = query.joins(:#{include_table})" if include_table.present?}

            query
          end
        eoruby
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
unico-training-7.8.0 lib/active_record/filter.rb