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
289 290 291 292 293 |
# File 'lib/datagrid/core.rb', line 289 def ==(other) self.class == other.class && attributes == other.attributes && scope == other.scope end |
#[](attribute) ⇒ Object
Returns Any datagrid attribute value.
196 197 198 |
# File 'lib/datagrid/core.rb', line 196 def [](attribute) public_send(attribute) end |
#[]=(attribute, value) ⇒ void
This method returns an undefined value.
Assigns any datagrid attribute
204 205 206 |
# File 'lib/datagrid/core.rb', line 204 def []=(attribute, value) public_send(:"#{attribute}=", value) end |
#as_query ⇒ Hash{Symbol => Object}
Returns serializable query arguments skipping all nil values.
217 218 219 220 221 222 223 |
# File 'lib/datagrid/core.rb', line 217 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.
209 210 211 |
# File 'lib/datagrid/core.rb', line 209 def assets scope end |
#attributes ⇒ Hash{Symbol => Object}
Returns grid attributes including filter values and ordering values.
186 187 188 189 190 191 192 |
# File 'lib/datagrid/core.rb', line 186 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.
165 166 167 168 169 170 171 172 173 174 |
# File 'lib/datagrid/core.rb', line 165 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.
282 283 284 285 286 287 |
# File 'lib/datagrid/core.rb', line 282 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.
230 231 232 |
# File 'lib/datagrid/core.rb', line 230 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.
272 273 274 |
# File 'lib/datagrid/core.rb', line 272 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.
296 297 298 |
# File 'lib/datagrid/core.rb', line 296 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.
267 268 269 |
# File 'lib/datagrid/core.rb', line 267 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.
247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/datagrid/core.rb', line 247 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 |