Module: Datagrid::Core::InstanceMethods
- Defined in:
- lib/datagrid/core.rb
Overview
ClassMethods
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#[](attribute) ⇒ Object
Alias for
send
method. - #[]=(attribute, value) ⇒ Object
-
#as_query ⇒ Object
Returns serializable query arguments skipping all nil values.
-
#assets ⇒ Object
Returns a scope(e.g ActiveRecord::Relation) with all applied filters.
-
#attributes ⇒ Object
Returns a hash of grid attributes including filter values and ordering values.
-
#attributes=(attributes) ⇒ Object
Updates datagrid attributes with a passed hash argument.
-
#check_scope_defined!(message = nil) ⇒ Object
:nodoc:.
-
#driver ⇒ Object
:nodoc:.
- #initialize(attributes = nil, &block) ⇒ Object
- #inspect ⇒ Object
-
#query_params(attributes = {}) ⇒ Object
Returns query parameters to link this grid from a page.
-
#redefined_scope? ⇒ Boolean
Returns true if the scope was redefined for this instance of grid object.
-
#reset_scope ⇒ Object
Resets current instance scope to default scope defined in a class.
-
#scope(&block) ⇒ Object
Redefines scope at instance level.
Instance Method Details
#==(other) ⇒ Object
238 239 240 241 242 |
# File 'lib/datagrid/core.rb', line 238 def ==(other) self.class == other.class && attributes == other.attributes && scope == other.scope end |
#[](attribute) ⇒ Object
Alias for send
method
136 137 138 |
# File 'lib/datagrid/core.rb', line 136 def [](attribute) self.send(attribute) end |
#[]=(attribute, value) ⇒ Object
140 141 142 |
# File 'lib/datagrid/core.rb', line 140 def []=(attribute, value) self.send(:"#{attribute}=", value) end |
#as_query ⇒ Object
Returns serializable query arguments skipping all nil values
grid = ProductsGrid.new(category: 'dresses', available: true)
grid.as_query # => {category: 'dresses', available: true}
169 170 171 172 173 174 175 |
# File 'lib/datagrid/core.rb', line 169 def as_query attributes = self.attributes.clone attributes.each do |key, value| attributes.delete(key) if value.nil? end attributes end |
#assets ⇒ Object
Returns a scope(e.g ActiveRecord::Relation) with all applied filters
145 146 147 |
# File 'lib/datagrid/core.rb', line 145 def assets driver.to_scope(scope) end |
#attributes ⇒ Object
Returns a hash of grid attributes including filter values and ordering values
127 128 129 130 131 132 133 |
# File 'lib/datagrid/core.rb', line 127 def attributes result = {} self.datagrid_attributes.each do |name| result[name] = self[name] end result end |
#attributes=(attributes) ⇒ Object
Updates datagrid attributes with a passed hash argument
151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/datagrid/core.rb', line 151 def attributes=(attributes) if respond_to?(:assign_attributes) if !forbidden_attributes_protection && attributes.respond_to?(:permit!) attributes.permit! end assign_attributes(attributes) else attributes.each do |name, value| self[name] = value end self end end |
#check_scope_defined!(message = nil) ⇒ Object
:nodoc:
227 228 229 |
# File 'lib/datagrid/core.rb', line 227 def check_scope_defined!( = nil) #:nodoc: self.class.send :check_scope_defined!, end |
#driver ⇒ Object
:nodoc:
223 224 225 |
# File 'lib/datagrid/core.rb', line 223 def driver #:nodoc: self.class.driver end |
#initialize(attributes = nil, &block) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/datagrid/core.rb', line 112 def initialize(attributes = nil, &block) super() if attributes self.attributes = attributes end instance_eval(&dynamic_block) if dynamic_block if block_given? self.scope(&block) end end |
#inspect ⇒ Object
231 232 233 234 235 236 |
# File 'lib/datagrid/core.rb', line 231 def inspect attrs = attributes.map do |key, value| "#{key}: #{value.inspect}" end.join(", ") "#<#{self.class} #{attrs}>" end |
#query_params(attributes = {}) ⇒ Object
Returns query parameters to link this grid from a page
grid = ProductsGrid.new(category: 'dresses', available: true)
Rails.application.routes.url_helpers.products_path(grid.query_params)
# => "/products?products_grid[category]=dresses&products_grid[available]=true"
182 183 184 |
# File 'lib/datagrid/core.rb', line 182 def query_params(attributes = {}) { param_name.to_sym => as_query.merge(attributes) } end |
#redefined_scope? ⇒ Boolean
Returns true if the scope was redefined for this instance of grid object
219 220 221 |
# File 'lib/datagrid/core.rb', line 219 def redefined_scope? self.class.scope_value != scope_value end |
#reset_scope ⇒ Object
Resets current instance scope to default scope defined in a class
214 215 216 |
# File 'lib/datagrid/core.rb', line 214 def reset_scope self.scope_value = self.class.scope_value end |
#scope(&block) ⇒ Object
Redefines scope at instance level
class MyGrid
scope { Article.order('created_at desc') }
end
grid = MyGrid.new
grid.scope do |scope|
scope.where(:author_id => current_user.id)
end
grid.assets
# => SELECT * FROM articles WHERE author_id = ?
# ORDER BY created_at desc
200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/datagrid/core.rb', line 200 def scope(&block) if block_given? current_scope = scope self.scope_value = proc { Datagrid::Utils.apply_args(current_scope, &block) } self else check_scope_defined! scope_value.call end end |