Sha256: 8afcfa7f5201dc764a2ac6d95ca6ba8f9bc1ffc4561db1d754b9a6b4576381bf

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

module Rails
  module AddOns
    module TableHelper
      def collection_table(options = {}, &block)
        Component::CollectionTable.new(self, options, &block).perform
      end

      def resource_table(options = {}, &block)
        Component::ResourceTable.new(self, options, &block).perform
      end

      def sort_link(column_name, title, options = {})
        return title if options === false
        SortLink.new(self, column_name, title, options).perform
      end

      class SortLink
        def initialize(view_context, column_name, title, options)
          default_options = {}

          if options === true
            @options = default_options
          else
            @options = options.reverse_merge(default_options)
          end

          @view_context = view_context
          @column_name  = @options[:column_name] || column_name
          @title        = title

          if h.params[:sort_direction].present?
            @sort_direction = (h.params[:sort_direction].to_sym == :asc) ? :desc : :asc
          else
            @sort_direction = :asc
          end
        end

        def perform
          h.link_to(@title, h.url_for(sort_by: @column_name, sort_direction: @sort_direction))
        end

        private

        def h
          @view_context
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails-add_ons-1.5.0 app/helpers/rails/add_ons/table_helper.rb