Sha256: e6f8d422c0a72c410ce69ae1838d20e176371b1b2a2d7d2339a031d63a9941fc

Contents?: true

Size: 1.09 KB

Versions: 20

Compression:

Stored size: 1.09 KB

Contents

module Snaptable
  module Constructor
    module Sortable

      def sortable(column)
        if model.reflect_on_association(column.gsub /_id/, '').nil?
          view_context.link_to({sort: column, direction: direction(column), query: params[:query], page: page}, {remote: true, class: css_class(column)}) do
            model.human_attribute_name(column)
          end
        else
          model.human_attribute_name(column)
        end
      end

      def sort_column
        column_names.include?(params[:sort]) ? column_name(params[:sort]) : column_name("id")
      end
      
      def sort_direction
        %w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"
      end

      def page
        params[:page] || 1
      end

      def css_class(column)
        column_name(column) == sort_column ? "current #{sort_direction}" : nil
      end

      def direction(column)
        column_name(column) == sort_column && sort_direction == "asc" ? "desc" : "asc"
      end

      private

      def column_name(column)
        "#{model.table_name}.#{column}"
      end

    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
snaptable-0.8.6 lib/snaptable/constructor/sortable.rb
snaptable-0.8.5 lib/snaptable/constructor/sortable.rb
snaptable-0.8.4 lib/snaptable/constructor/sortable.rb
snaptable-0.8.3 lib/snaptable/constructor/sortable.rb
snaptable-0.8.2 lib/snaptable/constructor/sortable.rb
snaptable-0.8.1 lib/snaptable/constructor/sortable.rb
snaptable-0.8.0 lib/snaptable/constructor/sortable.rb
snaptable-0.7.0 lib/snaptable/constructor/sortable.rb
snaptable-0.6.4 lib/snaptable/constructor/sortable.rb
snaptable-0.6.3 lib/snaptable/constructor/sortable.rb
snaptable-0.6.1 lib/snaptable/constructor/sortable.rb
snaptable-0.6.0 lib/snaptable/constructor/sortable.rb
snaptable-0.5.1 lib/snaptable/constructor/sortable.rb
snaptable-0.5.0 lib/snaptable/constructor/sortable.rb
snaptable-0.4.2 lib/snaptable/constructor/sortable.rb
snaptable-0.4.1 lib/snaptable/constructor/sortable.rb
snaptable-0.4.0 lib/snaptable/constructor/sortable.rb
snaptable-0.3.0 lib/snaptable/constructor/sortable.rb
snaptable-0.2.1 lib/snaptable/constructor/sortable.rb
snaptable-0.2.0 lib/snaptable/constructor/sortable.rb