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_json(options = nil) ⇒ Hash{String => Object}
JSON representation of datagrid attributes.
-
#as_query ⇒ Hash{String => 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
300 301 302 303 304 |
# File 'lib/datagrid/core.rb', line 300 def ==(other) self.class == other.class && attributes == other.attributes && scope == other.scope end |
#[](attribute) ⇒ Object
197 198 199 |
# File 'lib/datagrid/core.rb', line 197 def [](attribute) public_send(attribute) end |
#[]=(attribute, value) ⇒ void
This method returns an undefined value.
Assigns any datagrid attribute
205 206 207 |
# File 'lib/datagrid/core.rb', line 205 def []=(attribute, value) public_send(:"#{attribute}=", value) end |
#as_json(options = nil) ⇒ Hash{String => Object}
Returns JSON representation of datagrid attributes.
238 239 240 241 242 243 |
# File 'lib/datagrid/core.rb', line 238 def as_json( = nil) attributes.reduce({}) do |result, (k,v)| result[k.to_s] = v.as_json() result end end |
#as_query ⇒ Hash{String => Object}
Returns serializable query arguments skipping all nil values.
218 219 220 221 222 223 |
# File 'lib/datagrid/core.rb', line 218 def as_query self.attributes.reduce({}) do |result, (k,v)| result[k.to_s] = v.as_json unless v.nil? result end end |
#assets ⇒ Object
210 211 212 |
# File 'lib/datagrid/core.rb', line 210 def assets scope end |
#attributes ⇒ Hash{Symbol => Object}
Returns grid attributes including filter values and ordering values.
187 188 189 190 191 192 193 |
# File 'lib/datagrid/core.rb', line 187 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.
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/datagrid/core.rb', line 166 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
293 294 295 296 297 298 |
# File 'lib/datagrid/core.rb', line 293 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_s => as_query.merge(attributes.stringify_keys) } end |
#redefined_scope? ⇒ Boolean
283 284 285 |
# File 'lib/datagrid/core.rb', line 283 def redefined_scope? self.class.scope_value != scope_value end |
#reset ⇒ void
307 308 309 |
# File 'lib/datagrid/core.rb', line 307 def reset assets.reset end |
#reset_scope ⇒ void
278 279 280 |
# File 'lib/datagrid/core.rb', line 278 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.
258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/datagrid/core.rb', line 258 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 |