Sha256: 5207f5409ddf173ac2e8957a93aa5fa8d94a50e6315dda55580e9d8871186d22

Contents?: true

Size: 1.54 KB

Versions: 9

Compression:

Stored size: 1.54 KB

Contents

module ActiveGrid
  class Controller < ::ApplicationController
    helper do
      attr_accessor :_activegrid

      def activegrid(name_or_options = {}, &block)
        if name_or_options.is_a?(Hash)
          _activegrid.options.merge!(name_or_options)
          _activegrid.block = block
        else
          if grid = activegrids[name_or_options]
            grid.to_html
          else
            raise "No such activegrid #{name}"
          end
        end
      end
    end

    private
      def activegrid(name, scope, partial = "#{name}/grid")
        raise "scope must be an ActiveRecord::Relation" unless scope.is_a?(ActiveRecord::Relation)

        if params = self.params[:activegrid] and params[:name] == name.to_s
          scope = scope.order(params[:order]) if params[:order]
          scope = scope.limit(params[:limit]) if params[:limit]
          scope = scope.offset(params[:offset]) if params[:offset]

          if filter = params[:filter]
            table = scope.klass.arel_table
            scope = scope.where(table[filter[:field]].matches("%#{filter[:value]}%"))
          end

          @element = "#{name}_grid"
          @grid = ActiveGrid::Grid.new(view_context, name, scope, partial)

          respond_to do |format|
            format.js { render "active_grid/update" }
          end

          true
        else
          activegrids[name] = ActiveGrid::Grid.new(view_context, name, scope, partial)
          false
        end
      end

      def activegrids
        @activegrids ||= {}
      end
      helper_method :activegrids
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
activegrid-1.0.8 app/controllers/active_grid/controller.rb
activegrid-1.0.7 app/controllers/active_grid/controller.rb
activegrid-1.0.6 app/controllers/active_grid/controller.rb
activegrid-1.0.5 app/controllers/active_grid/controller.rb
activegrid-1.0.4 app/controllers/active_grid/controller.rb
activegrid-1.0.3 app/controllers/active_grid/controller.rb
activegrid-1.0.2 app/controllers/active_grid/controller.rb
activegrid-1.0.1 app/controllers/active_grid/controller.rb
activegrid-1.0.0 app/controllers/active_grid/controller.rb