Module: Datagrid::Core::InstanceMethods
- Defined in:
- lib/datagrid/core.rb
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#[](attribute) ⇒ Object
Any datagrid attribute value.
-
#[]=(attribute, value) ⇒ void
Assigns any datagrid attribute.
-
#as_query ⇒ Object
Returns 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.
-
#attributes=(attributes) ⇒ Object
Updates datagrid attributes with a passed hash argument.
- #initialize(attributes = nil, &block) ⇒ Object
-
#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_scope ⇒ void
Resets current instance scope to default scope defined in a class.
-
#scope(&block) ⇒ Object
Redefines scope at instance level.
Instance Method Details
#==(other) ⇒ Object
271 272 273 274 275 |
# File 'lib/datagrid/core.rb', line 271 def ==(other) self.class == other.class && attributes == other.attributes && scope == other.scope end |
#[](attribute) ⇒ Object
Returns Any datagrid attribute value.
157 158 159 |
# File 'lib/datagrid/core.rb', line 157 def [](attribute) self.send(attribute) end |
#[]=(attribute, value) ⇒ void
This method returns an undefined value.
Assigns any datagrid attribute
165 166 167 |
# File 'lib/datagrid/core.rb', line 165 def []=(attribute, value) self.send(:"#{attribute}=", value) end |
#as_query ⇒ Object
Returns serializable query arguments skipping all nil values
193 194 195 196 197 198 199 |
# File 'lib/datagrid/core.rb', line 193 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.
170 171 172 |
# File 'lib/datagrid/core.rb', line 170 def assets scope end |
#attributes ⇒ Hash<Symbol, Object>
Returns grid attributes including filter values and ordering values.
148 149 150 151 152 153 154 |
# File 'lib/datagrid/core.rb', line 148 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
175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/datagrid/core.rb', line 175 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 |
#initialize(attributes = nil, &block) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/datagrid/core.rb', line 134 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 ⇒ String
Returns a datagrid attributes and their values in inspection form.
264 265 266 267 268 269 |
# File 'lib/datagrid/core.rb', line 264 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.
206 207 208 |
# File 'lib/datagrid/core.rb', line 206 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.
249 250 251 |
# File 'lib/datagrid/core.rb', line 249 def redefined_scope? self.class.scope_value != scope_value end |
#reset_scope ⇒ void
This method returns an undefined value.
Resets current instance scope to default scope defined in a class
244 245 246 |
# File 'lib/datagrid/core.rb', line 244 def reset_scope self.scope_value = self.class.scope_value end |
#scope(&block) ⇒ Object
Redefines scope at instance level
223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/datagrid/core.rb', line 223 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 |