Module: Fancygrid::Controller::Helper

Includes:
ActionView::Helpers::TextHelper
Defined in:
lib/fancygrid/controller/helper.rb

Instance Method Summary collapse

Instance Method Details

#fancygrid_for(name, options = {}) {|instance| ... } ⇒ Object

Creates a fancygrid instance for the given model name

Yields:

  • (instance)


7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fancygrid/controller/helper.rb', line 7

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

#load_fancystate_hash(name) ⇒ Object

Loads a fancygrid state for given name



36
37
38
# File 'lib/fancygrid/controller/helper.rb', line 36

def load_fancystate_hash(name)
  session.fetch("fancygrid_#{name}", HashWithIndifferentAccess.new)
end

#resolve_fancystate_hash(name, persist_state = false) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fancygrid/controller/helper.rb', line 21

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

#store_fancystate_hash(name, state) ⇒ Object

Stores the given fancygrid state under given name



41
42
43
# File 'lib/fancygrid/controller/helper.rb', line 41

def store_fancystate_hash(name, state)
  session["fancygrid_#{name}"] = state
end