Sha256: e4b500b0f89c5d01ce682cc1e8cae5e56899b4f92e53f3ff8cf7715fcc599c31

Contents?: true

Size: 1.46 KB

Versions: 9

Compression:

Stored size: 1.46 KB

Contents

module Fancygrid  
  module Controller
    module Helper
      include ActionView::Helpers::TextHelper
            
      # Creates a fancygrid instance for the given model name
      def fancygrid_for(name, options = {})#:yields: grid
        persist_state = (options.delete(:persist) == true)
        state_hash = resolve_fancystate_hash(name, persist_state)
        
        klass = options.fetch(:builder, Fancygrid::Grid)
        instance = klass.new(name, options.merge(:state_hash => state_hash))      
        
        @fancygrid_collection ||= HashWithIndifferentAccess.new
        @fancygrid_collection[name] = instance
        
        yield instance if block_given?
        return instance
      end
    
      def resolve_fancystate_hash(name, persist_state = false)
        state_hash = params.fetch(:fancygrid, {}).fetch(name, nil)
        
        if persist_state
          if state_hash.nil?
            state_hash = load_fancystate_hash(name)
          else
            store_fancystate_hash(name, state_hash)
          end
        end
        
        return state_hash || HashWithIndifferentAccess.new
      end
        
      # Loads a fancygrid state for given name
      def load_fancystate_hash(name)
        session.fetch("fancygrid_#{name}", HashWithIndifferentAccess.new)
      end
          
      # Stores the given fancygrid state under given name
      def store_fancystate_hash(name, state)
        session["fancygrid_#{name}"] = state
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
fancygrid-2.0.8 lib/fancygrid/controller/helper.rb
fancygrid-2.0.7 lib/fancygrid/controller/helper.rb
fancygrid-2.0.6 lib/fancygrid/controller/helper.rb
fancygrid-2.0.5 lib/fancygrid/controller/helper.rb
fancygrid-2.0.4 lib/fancygrid/controller/helper.rb
fancygrid-2.0.3 lib/fancygrid/controller/helper.rb
fancygrid-2.0.2 lib/fancygrid/controller/helper.rb
fancygrid-2.0.1 lib/fancygrid/controller/helper.rb
fancygrid-2.0.0 lib/fancygrid/controller/helper.rb