Class: Fancygrid::ViewState
- Inherits:
-
Object
- Object
- Fancygrid::ViewState
- Defined in:
- lib/fancygrid/view_state.rb
Overview
{
:columns => [
{ :identifier => <string>, :visible => <bool>, :position => <number> }
]
:conditions => [
{ :identifier => <string>, :operator => <string>, :value => <string> }
],
:operator => ["all"|"any"],
:order =>
}
Instance Attribute Summary collapse
-
#dump ⇒ Object
Returns the value of attribute dump.
Instance Method Summary collapse
- #column_condition(node, option, default) ⇒ Object
- #column_conditions(node) ⇒ Object
- #column_option(node, option, default) ⇒ Object
- #column_options(node) ⇒ Object
- #column_order(node) ⇒ Object
- #conditions ⇒ Object
- #conditions_match_all? ⇒ Boolean
-
#initialize(dump = {}) ⇒ ViewState
constructor
A new instance of ViewState.
- #operator ⇒ Object
- #order ⇒ Object
- #order_column ⇒ Object
- #order_direction ⇒ Object
- #order_table ⇒ Object
- #ordered? ⇒ Boolean
- #pagination ⇒ Object
- #pagination_options(default_page, default_per_page) ⇒ Object
- #pagination_page(default = 1) ⇒ Object
- #pagination_per_page(default = 20) ⇒ Object
- #search_visible ⇒ Object
- #sql_order ⇒ Object
Constructor Details
#initialize(dump = {}) ⇒ ViewState
Returns a new instance of ViewState.
18 19 20 21 22 23 24 25 |
# File 'lib/fancygrid/view_state.rb', line 18 def initialize(dump = {}) raise ArgumentError, "'dump' must be a Hash" unless dump.is_a? Hash self.dump = dump.with_indifferent_access self.fix_columns self.fix_conditions self.fix_order self.fix_pagination end |
Instance Attribute Details
#dump ⇒ Object
Returns the value of attribute dump.
16 17 18 |
# File 'lib/fancygrid/view_state.rb', line 16 def dump @dump end |
Instance Method Details
#column_condition(node, option, default) ⇒ Object
43 44 45 |
# File 'lib/fancygrid/view_state.rb', line 43 def column_condition(node, option, default) self.column_conditions(node).fetch(option.to_s, default) end |
#column_conditions(node) ⇒ Object
37 38 39 40 41 |
# File 'lib/fancygrid/view_state.rb', line 37 def column_conditions(node) self.dump[:conditions].select { |hash| node.identifier == hash[:identifier] }.first || {} end |
#column_option(node, option, default) ⇒ Object
33 34 35 |
# File 'lib/fancygrid/view_state.rb', line 33 def column_option(node, option, default) self.(node).fetch(option, default) end |
#column_options(node) ⇒ Object
27 28 29 30 31 |
# File 'lib/fancygrid/view_state.rb', line 27 def (node) self.dump[:columns].select { |hash| node.identifier == hash[:identifier] }.first || {} end |
#column_order(node) ⇒ Object
75 76 77 78 79 |
# File 'lib/fancygrid/view_state.rb', line 75 def column_order(node) if node.identifier.to_s == self.order[:identifier].to_s self.order_direction end end |
#conditions ⇒ Object
47 48 49 |
# File 'lib/fancygrid/view_state.rb', line 47 def conditions self.dump[:conditions] end |
#conditions_match_all? ⇒ Boolean
55 56 57 |
# File 'lib/fancygrid/view_state.rb', line 55 def conditions_match_all? self.dump.fetch(:conditions_match, :any).to_s == "all" end |
#operator ⇒ Object
51 52 53 |
# File 'lib/fancygrid/view_state.rb', line 51 def operator self.dump[:operator] end |
#order ⇒ Object
59 60 61 |
# File 'lib/fancygrid/view_state.rb', line 59 def order self.dump.fetch(:order, {}) end |
#order_column ⇒ Object
67 68 69 |
# File 'lib/fancygrid/view_state.rb', line 67 def order_column self.order[:identifier].to_s.split(".").last end |
#order_direction ⇒ Object
71 72 73 |
# File 'lib/fancygrid/view_state.rb', line 71 def order_direction self.order[:direction] end |
#order_table ⇒ Object
63 64 65 |
# File 'lib/fancygrid/view_state.rb', line 63 def order_table self.order[:identifier].to_s.split(".").first end |
#ordered? ⇒ Boolean
81 82 83 |
# File 'lib/fancygrid/view_state.rb', line 81 def ordered? !(order_table.blank? || order_column.blank? || order_direction.blank?) end |
#pagination ⇒ Object
90 91 92 |
# File 'lib/fancygrid/view_state.rb', line 90 def pagination self.dump.fetch(:pagination, {}) end |
#pagination_options(default_page, default_per_page) ⇒ Object
106 107 108 109 110 111 |
# File 'lib/fancygrid/view_state.rb', line 106 def (default_page, default_per_page) { :page => self.pagination_page(default_page).to_i, :per_page => self.pagination_per_page(default_per_page).to_i } end |
#pagination_page(default = 1) ⇒ Object
94 95 96 97 98 |
# File 'lib/fancygrid/view_state.rb', line 94 def pagination_page default=1 result = self.pagination.fetch(:page, default) return default if result <= 0 return result end |
#pagination_per_page(default = 20) ⇒ Object
100 101 102 103 104 |
# File 'lib/fancygrid/view_state.rb', line 100 def pagination_per_page default=20 result = self.pagination.fetch(:per_page, default) return default if result <= 0 return result end |
#search_visible ⇒ Object
113 114 115 |
# File 'lib/fancygrid/view_state.rb', line 113 def search_visible self.dump.fetch(:search_visible, false) end |
#sql_order ⇒ Object
85 86 87 88 |
# File 'lib/fancygrid/view_state.rb', line 85 def sql_order return nil unless ordered? return "#{order_table}.#{order_column} #{order_direction}" end |