Module: Datagrid::Core::InstanceMethods

Defined in:
lib/datagrid/core.rb

Instance Method Summary collapse

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.

Returns:

  • (Object)

    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

Parameters:

  • attribute (Symbol, String)

    Datagrid attribute name

  • value (Object)

    Datagrid attribute value



165
166
167
# File 'lib/datagrid/core.rb', line 165

def []=(attribute, value)
  self.send(:"#{attribute}=", value)
end

#as_queryObject

Returns serializable query arguments skipping all nil values

Examples:

grid = ProductsGrid.new(category: 'dresses', available: true)
grid.as_query # => {category: 'dresses', available: true}


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

#assetsObject

Returns a scope relation (e.g ActiveRecord::Relation) with all applied filters.

Returns:

  • (Object)

    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

#attributesHash<Symbol, Object>

Returns grid attributes including filter values and ordering values.

Returns:

  • (Hash<Symbol, Object>)

    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

#inspectString

Returns a datagrid attributes and their values in inspection form.

Returns:

  • (String)

    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.

Examples:

grid = ProductsGrid.new(category: 'dresses', available: true)
Rails.application.routes.url_helpers.products_path(grid.query_params)
  # => "/products?products_grid[category]=dresses&products_grid[available]=true"

Returns:

  • (Hash<Symbol, Hash<Symbol, Object>>)

    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.

Returns:

  • (Boolean)

    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_scopevoid

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

Examples:

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


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