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
-
#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
226 227 228 229 230 |
# File 'lib/datagrid/core.rb', line 226 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
166 167 168 169 170 171 172 |
# File 'lib/datagrid/core.rb', line 166 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:
215 216 217 |
# File 'lib/datagrid/core.rb', line 215 def check_scope_defined!( = nil) #:nodoc: self.class.send :check_scope_defined!, end |
#driver ⇒ Object
:nodoc:
211 212 213 |
# File 'lib/datagrid/core.rb', line 211 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
219 220 221 222 223 224 |
# File 'lib/datagrid/core.rb', line 219 def inspect attrs = attributes.map do |key, value| "#{key}: #{value.inspect}" end.join(", ") "#<#{self.class} #{attrs}>" end |
#redefined_scope? ⇒ Boolean
Returns true if the scope was redefined for this instance of grid object
207 208 209 |
# File 'lib/datagrid/core.rb', line 207 def redefined_scope? self.class.scope_value != scope_value end |
#reset_scope ⇒ Object
Resets current instance scope to default scope defined in a class
202 203 204 |
# File 'lib/datagrid/core.rb', line 202 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
188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/datagrid/core.rb', line 188 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 |