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, :search_button => false, :searcher_size => nil, }
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#current_order ⇒ Object
readonly
Returns the value of attribute current_order.
-
#current_sort_col ⇒ Object
readonly
Returns the value of attribute current_sort_col.
-
#default_order ⇒ Object
readonly
Returns the value of attribute default_order.
-
#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
- #base_params ⇒ Object
- #collection ⇒ Object
- #current_page ⇒ Object
- #current_search ⇒ Object
-
#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
- #order(something) ⇒ Object
- #order_sql(something) ⇒ Object
- #param(key, default = nil) ⇒ Object
- #param_key(key) ⇒ Object
- #searchable? ⇒ Boolean
- #searcher ⇒ Object
Constructor Details
#initialize(cols_or_opts, collection = nil, controller = nil, opts = {}) ⇒ Definition
Returns a new instance of Definition.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/magic_grid/definition.rb', line 60 def initialize(cols_or_opts, collection = nil, controller = nil, opts = {}) , @columns = *self.class.(cols_or_opts, opts) @default_order = [:default_order] @params = controller && controller.params || {} @collection = Collection.create_or_reuse collection, @columns = Column.columns_for_collection(@collection, @columns, [:searchable]) @current_sort_col = param(:col, [:default_col]).to_i unless (0...@columns.count).cover? @current_sort_col @current_sort_col = [:default_col] end @current_order = order(param(:order, @default_order)) @collection.apply_sort(@columns[@current_sort_col], order_sql(@current_order)) filter_keys = [:listeners].values filters = @params.slice(*filter_keys).reject {|k,v| v.to_s.empty? } @collection.apply_filter filters @collection.apply_pagination(current_page) @collection.apply_search current_search @collection.per_page = [:per_page] @collection.apply_filter_callback [:listener_handler] @collection.enable_post_filter [:collection_post_filter] @collection.add_post_filter_callback [:post_filter] end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
8 9 10 |
# File 'lib/magic_grid/definition.rb', line 8 def columns @columns end |
#current_order ⇒ Object (readonly)
Returns the value of attribute current_order.
8 9 10 |
# File 'lib/magic_grid/definition.rb', line 8 def current_order @current_order end |
#current_sort_col ⇒ Object (readonly)
Returns the value of attribute current_sort_col.
8 9 10 |
# File 'lib/magic_grid/definition.rb', line 8 def current_sort_col @current_sort_col end |
#default_order ⇒ Object (readonly)
Returns the value of attribute default_order.
8 9 10 |
# File 'lib/magic_grid/definition.rb', line 8 def default_order @default_order end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/magic_grid/definition.rb', line 8 def end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
8 9 10 |
# File 'lib/magic_grid/definition.rb', line 8 def params @params end |
Class Method Details
.normalize_columns_options(cols_or_opts, opts) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/magic_grid/definition.rb', line 47 def self.(cols_or_opts, opts) if cols_or_opts.is_a? Hash = runtime_defaults.merge(cols_or_opts.reject {|k| k == :cols}) columns = cols_or_opts.fetch(:cols, []) elsif cols_or_opts.is_a? 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
37 38 39 40 41 42 43 44 45 |
# File 'lib/magic_grid/definition.rb', line 37 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
#base_params ⇒ Object
122 123 124 |
# File 'lib/magic_grid/definition.rb', line 122 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_page ⇒ Object
126 127 128 |
# File 'lib/magic_grid/definition.rb', line 126 def current_page [param(:page, 1).to_i, 1].max end |
#current_search ⇒ Object
90 91 92 |
# File 'lib/magic_grid/definition.rb', line 90 def current_search param(:q) end |
#magic_collection ⇒ Object
11 12 13 |
# File 'lib/magic_grid/definition.rb', line 11 def magic_collection @collection end |
#magic_id ⇒ Object
94 95 96 |
# File 'lib/magic_grid/definition.rb', line 94 def magic_id [:id] || (Column.hash_string(@columns) + @collection.hash_string) end |
#needs_searcher? ⇒ Boolean
102 103 104 |
# File 'lib/magic_grid/definition.rb', line 102 def needs_searcher? [:needs_searcher] or (searchable? and not [:searcher]) end |
#order(something) ⇒ Object
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/magic_grid/definition.rb', line 130 def order(something) case something when 1, "1", :desc, :DESC, "desc", "DESC" 1 #when 0, "0", :asc, :ASC, "asc", "ASC" # 0 else 0 end end |
#order_sql(something) ⇒ Object
141 142 143 |
# File 'lib/magic_grid/definition.rb', line 141 def order_sql(something) ["ASC", "DESC"][order(something)] end |
#param(key, default = nil) ⇒ Object
118 119 120 |
# File 'lib/magic_grid/definition.rb', line 118 def param(key, default=nil) @params.fetch(param_key(key), default) end |
#param_key(key) ⇒ Object
114 115 116 |
# File 'lib/magic_grid/definition.rb', line 114 def param_key(key) "#{magic_id}_#{key}".to_sym end |
#searchable? ⇒ Boolean
98 99 100 |
# File 'lib/magic_grid/definition.rb', line 98 def searchable? @collection.searchable? end |
#searcher ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/magic_grid/definition.rb', line 106 def searcher if needs_searcher? param_key(:searcher) else [:searcher] end end |