Class: MagicGrid::Definition
- Inherits:
-
Object
- Object
- MagicGrid::Definition
- Defined in:
- lib/magic_grid/definition.rb
Constant Summary collapse
- DEFAULTS =
{ :class => [], :top_pager => false, :bottom_pager => true, :remote => false, :min_search_length => 3, :id => false, :searcher => false, :needs_searcher => false, :live_search => false, :listeners => {}, :collapse_empty_header => false, :collapse_empty_footer => false, :default_ajax_handler => true, :default_order => :asc, :search_button => false, :searcher_size => nil, :title => nil, }
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Class Method Summary collapse
Instance Method Summary collapse
- #apply_collection_params ⇒ Object
- #base_params ⇒ Object
- #collection ⇒ Object
- #current_order ⇒ Object
- #current_page ⇒ Object
- #current_search ⇒ Object
- #current_sort_col ⇒ Object
- #default_order ⇒ Object
- #filters ⇒ Object
- #has_title? ⇒ Boolean
-
#initialize(cols_or_opts, collection = nil, controller = nil, opts = {}) ⇒ Definition
constructor
A new instance of Definition.
- #magic_collection ⇒ Object
- #magic_id ⇒ Object
- #needs_searcher? ⇒ Boolean
- #param(key, default = nil) ⇒ Object
- #param_key(key) ⇒ Object
- #searchable? ⇒ Boolean
- #searcher ⇒ Object
- #title ⇒ Object
Constructor Details
#initialize(cols_or_opts, collection = nil, controller = nil, opts = {}) ⇒ Definition
Returns a new instance of Definition.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/magic_grid/definition.rb', line 63 def initialize(cols_or_opts, collection = nil, controller = nil, opts = {}) @options, @columns = *self.class.(cols_or_opts, opts) @params = controller && controller.params || {} @collection = Collection.create_or_reuse collection, @columns = Column.columns_for_collection(magic_collection, columns, [:searchable]) columns.each do |col| if col.sortable? if col.id == current_sort_col col.order = current_order else col.order = Order::Unordered end else col.order = Order::Unsortable end end apply_collection_params end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
9 10 11 |
# File 'lib/magic_grid/definition.rb', line 9 def columns @columns end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/magic_grid/definition.rb', line 9 def @options end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
9 10 11 |
# File 'lib/magic_grid/definition.rb', line 9 def params @params end |
Class Method Details
.normalize_columns_options(cols_or_opts, opts) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/magic_grid/definition.rb', line 49 def self.(cols_or_opts, opts) case cols_or_opts when Hash = runtime_defaults.merge(cols_or_opts.reject {|k| k == :cols}) columns = cols_or_opts.fetch(:cols, []) when Array = runtime_defaults.merge opts columns = cols_or_opts else raise "I have no idea what that is, but it's not a columns list or options hash" end [, columns] end |
.runtime_defaults ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/magic_grid/definition.rb', line 39 def self.runtime_defaults # run these lazily to catch any late I18n path changes DEFAULTS.merge(Collection::DEFAULTS).merge( :if_empty => I18n.t("magic_grid.no_results").capitalize, # "No results found." :searcher_label => I18n.t("magic_grid.search.label").capitalize + ': ', # "Search: " :searcher_tooltip => I18n.t("magic_grid.search.tooltip"), # "type.. + <return>" :searcher_button => I18n.t("magic_grid.search.button").capitalize # "Search" ) end |
Instance Method Details
#apply_collection_params ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/magic_grid/definition.rb', line 87 def apply_collection_params magic_collection.apply_sort(columns[current_sort_col], current_order.to_sql) magic_collection.apply_filter filters magic_collection.apply_pagination(current_page) magic_collection.apply_search current_search magic_collection.per_page = [:per_page] magic_collection.apply_filter_callback [:listener_handler] magic_collection.enable_post_filter [:collection_post_filter] magic_collection.add_post_filter_callback [:post_filter] end |
#base_params ⇒ Object
166 167 168 |
# File 'lib/magic_grid/definition.rb', line 166 def base_params params.merge :magic_grid_id => magic_id end |
#collection ⇒ Object
15 16 17 |
# File 'lib/magic_grid/definition.rb', line 15 def collection @collection.collection end |
#current_order ⇒ Object
122 123 124 |
# File 'lib/magic_grid/definition.rb', line 122 def current_order @current_order ||= Order.from_param(param(:order, default_order.to_param)) end |
#current_page ⇒ Object
170 171 172 |
# File 'lib/magic_grid/definition.rb', line 170 def current_page [param(:page, 1).to_i, 1].max end |
#current_search ⇒ Object
126 127 128 |
# File 'lib/magic_grid/definition.rb', line 126 def current_search param(:q, "") end |
#current_sort_col ⇒ Object
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/magic_grid/definition.rb', line 107 def current_sort_col @current_sort_col ||= begin given = param(:col, -1).to_i if given >= 0 and given <= columns.count given else [:default_col].to_i end end end |
#default_order ⇒ Object
118 119 120 |
# File 'lib/magic_grid/definition.rb', line 118 def default_order @default_order ||= Order.from_param([:default_order]) end |
#filters ⇒ Object
100 101 102 103 104 105 |
# File 'lib/magic_grid/definition.rb', line 100 def filters @filters ||= begin filter_keys = [:listeners].values params.slice(*filter_keys).reject {|k,v| v.to_s.empty? } end end |
#has_title? ⇒ Boolean
150 151 152 |
# File 'lib/magic_grid/definition.rb', line 150 def has_title? [:title] || false end |
#magic_collection ⇒ Object
11 12 13 |
# File 'lib/magic_grid/definition.rb', line 11 def magic_collection @collection end |
#magic_id ⇒ Object
130 131 132 |
# File 'lib/magic_grid/definition.rb', line 130 def magic_id [:id] || (Column.hash_string(columns) + magic_collection.hash_string) end |
#needs_searcher? ⇒ Boolean
138 139 140 |
# File 'lib/magic_grid/definition.rb', line 138 def needs_searcher? [:needs_searcher] or (searchable? and not [:searcher]) end |
#param(key, default = nil) ⇒ Object
162 163 164 |
# File 'lib/magic_grid/definition.rb', line 162 def param(key, default=nil) params.fetch(param_key(key), default) end |
#param_key(key) ⇒ Object
158 159 160 |
# File 'lib/magic_grid/definition.rb', line 158 def param_key(key) "#{magic_id}_#{key}".to_sym end |
#searchable? ⇒ Boolean
134 135 136 |
# File 'lib/magic_grid/definition.rb', line 134 def searchable? magic_collection.searchable? end |
#searcher ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/magic_grid/definition.rb', line 142 def searcher if needs_searcher? param_key(:searcher) else [:searcher] end end |
#title ⇒ Object
154 155 156 |
# File 'lib/magic_grid/definition.rb', line 154 def title [:title] end |