Module: Datagrid::Core::InstanceMethods
- Defined in:
- lib/datagrid/core.rb
Overview
ClassMethods
Instance Method Summary collapse
-
#[](attribute) ⇒ Object
Alias for
send
method. - #[]=(attribute, value) ⇒ Object
- #as_query ⇒ Object
-
#assets ⇒ Object
Returns a scope(e.g ActiveRecord::Relation) with all applied filters.
-
#assign_attributes(attributes) ⇒ Object
(also: #attributes=)
Updates datagrid attributes with a passed hash argument.
-
#attributes ⇒ Object
Returns a hash of grid attributes including filter values and ordering values.
-
#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
#[](attribute) ⇒ Object
Alias for send
method
91 92 93 |
# File 'lib/datagrid/core.rb', line 91 def [](attribute) self.send(attribute) end |
#[]=(attribute, value) ⇒ Object
95 96 97 |
# File 'lib/datagrid/core.rb', line 95 def []=(attribute, value) self.send(:"#{attribute}=", value) end |
#as_query ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/datagrid/core.rb', line 114 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
100 101 102 |
# File 'lib/datagrid/core.rb', line 100 def assets driver.to_scope(scope) end |
#assign_attributes(attributes) ⇒ Object Also known as: attributes=
Updates datagrid attributes with a passed hash argument
106 107 108 109 110 111 |
# File 'lib/datagrid/core.rb', line 106 def assign_attributes(attributes) attributes.each do |name, value| self[name] = value end self end |
#attributes ⇒ Object
Returns a hash of grid attributes including filter values and ordering values
82 83 84 85 86 87 88 |
# File 'lib/datagrid/core.rb', line 82 def attributes result = {} self.datagrid_attributes.each do |name| result[name] = self[name] end result end |
#check_scope_defined!(message = nil) ⇒ Object
:nodoc:
163 164 165 |
# File 'lib/datagrid/core.rb', line 163 def check_scope_defined!( = nil) #:nodoc: self.class.send :check_scope_defined!, end |
#driver ⇒ Object
:nodoc:
159 160 161 |
# File 'lib/datagrid/core.rb', line 159 def driver #:nodoc: self.class.driver end |
#initialize(attributes = nil, &block) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/datagrid/core.rb', line 68 def initialize(attributes = nil, &block) super() if attributes self.attributes = attributes end if block_given? self.scope(&block) end end |
#inspect ⇒ Object
167 168 169 170 171 172 |
# File 'lib/datagrid/core.rb', line 167 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
155 156 157 |
# File 'lib/datagrid/core.rb', line 155 def redefined_scope? self.class.scope_value != scope_value end |
#reset_scope ⇒ Object
Resets current instance scope to default scope defined in a class
150 151 152 |
# File 'lib/datagrid/core.rb', line 150 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
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/datagrid/core.rb', line 136 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 |