Module: Datagrid::Core::InstanceMethods

Defined in:
lib/datagrid/core.rb

Overview

ClassMethods

Instance Method Summary collapse

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_queryObject



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

#assetsObject

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

#attributesObject

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!(message = nil) #:nodoc:
  self.class.send :check_scope_defined!, message
end

#driverObject

: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

#inspectObject



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

Returns:

  • (Boolean)


155
156
157
# File 'lib/datagrid/core.rb', line 155

def redefined_scope?
  self.class.scope_value != scope_value
end

#reset_scopeObject

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