Module: Datagrid::Core
Overview
Simple example of using Datagrid scope as the assets source to be queried from the database.
In most cases, the scope is a model class with some default ORM scopes, like order
or includes
:
The scope is also used to:
- Choose an ORM driver (e.g., Mongoid, ActiveRecord, etc.).
- Association preloading
- Columns Providing default order
You can set the scope at class level or instance level. Both having appropriate use cases
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#[](attribute) ⇒ Object
Any datagrid attribute value.
-
#[]=(attribute, value) ⇒ void
Assigns any datagrid attribute.
-
#as_query ⇒ Hash{Symbol => Object}
Serializable query arguments skipping all nil values.
-
#assets ⇒ Object
A scope relation (e.g ActiveRecord::Relation) with all applied filters.
-
#attributes ⇒ Hash{Symbol => Object}
Grid attributes including filter values and ordering values.
-
#initialize(attributes = nil) {|block| ... } ⇒ void
Initializes a new instance with optional attributes and an optional block.
-
#inspect ⇒ String
A datagrid attributes and their values in inspection form.
-
#query_params(attributes = {}) ⇒ Hash{Symbol => Hash{Symbol => Object}}
Query parameters to link this grid from a page.
-
#redefined_scope? ⇒ Boolean
True if the scope was redefined for this instance of grid object.
-
#reset ⇒ void
Resets loaded assets and column values cache.
-
#reset_scope ⇒ void
Resets current instance scope to default scope defined in a class.
-
#scope(&block) ⇒ void
Redefines scope at instance level.
Instance Method Details
#==(other) ⇒ Object
288 289 290 291 292 |
# File 'lib/datagrid/core.rb', line 288 def ==(other) self.class == other.class && attributes == other.attributes && scope == other.scope end |
#[](attribute) ⇒ Object
Returns Any datagrid attribute value.
195 196 197 |
# File 'lib/datagrid/core.rb', line 195 def [](attribute) public_send(attribute) end |
#[]=(attribute, value) ⇒ void
This method returns an undefined value.
Assigns any datagrid attribute
203 204 205 |
# File 'lib/datagrid/core.rb', line 203 def []=(attribute, value) public_send(:"#{attribute}=", value) end |
#as_query ⇒ Hash{Symbol => Object}
Returns serializable query arguments skipping all nil values.
216 217 218 219 220 221 222 |
# File 'lib/datagrid/core.rb', line 216 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 relation (e.g ActiveRecord::Relation) with all applied filters.
208 209 210 |
# File 'lib/datagrid/core.rb', line 208 def assets scope end |
#attributes ⇒ Hash{Symbol => Object}
Returns grid attributes including filter values and ordering values.
185 186 187 188 189 190 191 |
# File 'lib/datagrid/core.rb', line 185 def attributes result = {} datagrid_attributes.each do |name| result[name] = self[name] end result end |
#initialize(attributes = nil) {|block| ... } ⇒ void
Returns Initializes a new instance with optional attributes and an optional block.
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/datagrid/core.rb', line 164 def initialize(attributes = nil, &block) super() self.attributes = attributes if attributes instance_eval(&dynamic_block) if dynamic_block return unless block_given? scope(&block) end |
#inspect ⇒ String
Returns a datagrid attributes and their values in inspection form.
281 282 283 284 285 286 |
# File 'lib/datagrid/core.rb', line 281 def inspect attrs = attributes.map do |key, value| "#{key}: #{value.inspect}" end.join(", ") "#<#{self.class} #{attrs}>" end |
#query_params(attributes = {}) ⇒ Hash{Symbol => Hash{Symbol => Object}}
Returns query parameters to link this grid from a page.
229 230 231 |
# File 'lib/datagrid/core.rb', line 229 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.
271 272 273 |
# File 'lib/datagrid/core.rb', line 271 def redefined_scope? self.class.scope_value != scope_value end |
#reset ⇒ void
This method returns an undefined value.
Returns Resets loaded assets and column values cache.
295 296 297 |
# File 'lib/datagrid/core.rb', line 295 def reset assets.reset end |
#reset_scope ⇒ void
This method returns an undefined value.
Returns Resets current instance scope to default scope defined in a class.
266 267 268 |
# File 'lib/datagrid/core.rb', line 266 def reset_scope self.scope_value = self.class.scope_value end |
#scope(&block) ⇒ void
This method returns an undefined value.
Returns redefines scope at instance level.
246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/datagrid/core.rb', line 246 def scope(&block) if block_given? current_scope = scope self.scope_value = proc { Datagrid::Utils.apply_args(current_scope, &block) } self else scope = original_scope driver.to_scope(scope) end end |