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



87
88
89
# File 'lib/datagrid/core.rb', line 87

def [](attribute)
  self.send(attribute)
end

#[]=(attribute, value) ⇒ Object



91
92
93
# File 'lib/datagrid/core.rb', line 91

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

#as_queryObject



109
110
111
112
113
114
115
# File 'lib/datagrid/core.rb', line 109

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



96
97
98
# File 'lib/datagrid/core.rb', line 96

def assets
  driver.to_scope(scope)
end

#assign_attributes(attributes) ⇒ Object Also known as: attributes=



101
102
103
104
105
106
# File 'lib/datagrid/core.rb', line 101

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



78
79
80
81
82
83
84
# File 'lib/datagrid/core.rb', line 78

def attributes
  result = {}
  self.datagrid_attributes.each do |name|
    result[name] = self[name]
  end
  result
end

#check_scope_defined!(message = nil) ⇒ Object

:nodoc:



158
159
160
# File 'lib/datagrid/core.rb', line 158

def check_scope_defined!(message = nil) #:nodoc:
  self.class.send :check_scope_defined!, message
end

#driverObject

:nodoc:



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

def driver #:nodoc:
  self.class.driver
end

#initialize(attributes = nil, &block) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/datagrid/core.rb', line 64

def initialize(attributes = nil, &block)
  super()

  if attributes
    self.attributes = attributes
  end

  if block_given?
    self.scope(&block)
  end
end

#paginate(*args, &block) ⇒ Object

:nodoc:



117
118
119
120
# File 'lib/datagrid/core.rb', line 117

def paginate(*args, &block) # :nodoc:
  ::Datagrid::Utils.warn_once("#paginate is deprecated. Call it like object.assets.paginate(...).")
  self.assets.paginate(*args, &block)
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
  scope(&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