Class: Netzke::Basepack::DataAdapters::AbstractAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/netzke/basepack/data_adapters/abstract_adapter.rb

Overview

A concrete adapter should implement all the public instance methods of this adapter in order to support all the functionality of Basepack components.

Direct Known Subclasses

ActiveRecordAdapter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ AbstractAdapter

Returns a new instance of AbstractAdapter



266
267
268
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 266

def initialize(model)
  @model = model
end

Instance Attribute Details

#modelObject

Returns the value of attribute model



4
5
6
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 4

def model
  @model
end

Class Method Details

.adapter_class(model) ⇒ Object



262
263
264
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 262

def self.adapter_class(model)
  @subclasses.detect { |subclass| subclass.for_class?(model) } || AbstractAdapter
end

.for_class?(member_class) ⇒ Boolean

Used to determine if the given adapter should be used for the passed in class.

Returns:

  • (Boolean)


253
254
255
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 253

def self.for_class?(member_class)
  false # override in subclass
end

.inherited(subclass) ⇒ Object



257
258
259
260
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 257

def self.inherited(subclass)
  @subclasses ||= []
  @subclasses << subclass
end

Instance Method Details

#assoc_values(r, attr_hash) ⇒ Object

Returns a hash of association values for given record, e.g.:

{author__first_name: "Michael"}


204
205
206
207
208
209
210
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 204

def assoc_values(r, attr_hash) #:nodoc:
  {}.tap do |values|
    attr_hash.each_pair do |name,c|
      values[name] = record_value_for_attribute(r, c, true) if association_attr?(c)
    end
  end
end

#association_attr?(attr) ⇒ Boolean

Whether an attribute (by name) is an association one

Returns:

  • (Boolean)


185
186
187
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 185

def association_attr?(attr)
  !!attr[:name].to_s.index("__")
end

#attr_type(attr_name) ⇒ Object

Returns attribute type (as Symbol) given its name.



29
30
31
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 29

def attr_type(attr_name)
  :string
end

#attribute_namesObject

List of model attribute names as strings



17
18
19
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 17

def attribute_names
  []
end

#class_for(assoc_name) ⇒ Object

Returns the model class for association columns

Raises:

  • (NotImplementedError)


140
141
142
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 140

def class_for assoc_name
  raise NotImplementedError
end

#combo_data(attr, query = "") ⇒ Object

Returns options for comboboxes in grids/forms attr - column/field configuration; note that it will in its turn provide:

  • name - attribute name

  • scope - searching scope (optional)

query - whatever is entered in the combobox

Raises:

  • (NotImplementedError)


130
131
132
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 130

def combo_data(attr, query = "")
  raise NotImplementedError
end

#count_records(params, columns) ⇒ Object

Returns record count based on passed params. Implements:

  • filtering

  • scopes

`params` is a hash that contains the following keys:

  • :scope - the scope as described in Netzke::Grid::Base

  • :filter - Ext filters

The `columns` parameter may be used to use joins to address the n+1 query problem, and receives an array of column configurations



82
83
84
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 82

def count_records(params, columns)
  0
end

#destroy(ids) ⇒ Object

Destroys records with the provided ids



145
146
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 145

def destroy(ids)
end

#errors_array(record) ⇒ Object

give the data adapter the opporunity to process error messages must return an raay of the form [“Title can't be blank”, “Foo can't be blank”]



180
181
182
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 180

def errors_array(record)
  record.errors.to_a
end

#find_record(id) ⇒ Object

Finds a record by id, return nil if not found



149
150
151
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 149

def find_record(id)
  nil
end

#find_record_children(r, scope = nil) ⇒ Object



233
234
235
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 233

def find_record_children(r, scope = nil)
  extend_relation_with_scope(r.children, scope)
end

#find_root_records(scope = nil) ⇒ Object



237
238
239
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 237

def find_root_records(scope = nil)
  extend_relation_with_scope(model.where(parent_id: nil), scope)
end

#firstObject

gets the first record



68
69
70
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 68

def first
  nil
end

#foreign_key_for(assoc_name) ⇒ Object

Returns the foreign key name for an association

Raises:

  • (NotImplementedError)


135
136
137
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 135

def foreign_key_for assoc_name
  raise NotImplementedError
end

#get_assoc_property_type(assoc_name, prop_name) ⇒ Object

gets the type of a model attribute for xtype mapping i.e. get_assoc_property_type :author,:first_name should return :string Possible types to return :integer :boolean :date :datetime :time :text :string

Raises:

  • (NotImplementedError)


111
112
113
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 111

def get_assoc_property_type assoc_name, prop_name
  raise NotImplementedError
end

#get_property_type(column) ⇒ Object

like get_assoc_property_type but for non-association columns



116
117
118
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 116

def get_property_type column
  column.type
end

#get_records(params, columns) ⇒ Object

Returns records based on passed params. Implements:

  • pagination

  • filtering

  • scopes

`params` is a hash that contains the following keys:

sorters

sorting params, which is an array of hashes that contain the following keys in their turn:

property

the field that is being sorted on

direction

“asc” or “desc”

limit

rows per page in pagination

start

page number in pagination

scope

the scope as described in Netzke::Grid::Base

filters

an array of hashes representing a filter query, where the hashes have the following keys:

attr

Name of the (virtual) model attribute to apply the filter to

operator

Operator for this filter. Possible values are: contains, eq, gt, gteq, lt, lteq

value

The value for this filter

query

The `columns` parameter may be used to use joins to address the n+1 query problem, and receives an array of column configurations



63
64
65
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 63

def get_records(params, columns)
  []
end

#hash_fk_modelObject

Build a hash of foreign keys and the associated model

Raises:

  • (NotImplementedError)


154
155
156
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 154

def hash_fk_model
  raise NotImplementedError
end

#human_attribute_name(name) ⇒ Object

Returns human attribute name



224
225
226
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 224

def human_attribute_name(name)
  name.to_s.humanize
end

#map_type(type) ⇒ Object

Map a ORM type to a type symbol Possible types to return :integer :boolean :date :datetime :time :text :string

Default implementation works for ActiveRecord



97
98
99
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 97

def map_type type
  type
end

#model_attributesObject

Returns a list of model attribute names. For association columns the name can have the double-underscore format, e.g.: `author__first_name`. These attributes will be used by grids and forms to display default columns/fields.



24
25
26
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 24

def model_attributes
  []
end

#model_respond_to?(method) ⇒ Boolean

Does record respond to given method?

Returns:

  • (Boolean)


242
243
244
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 242

def model_respond_to?(method)
  @model.instance_methods.include?(method)
end

#move_records(params) ⇒ Object

Changes records position (e.g. when acts_as_list is used in ActiveRecord).

`params` is a hash with the following keys:

  • :ids - ids of records to move

  • :new_index - new starting position for the records to move



164
165
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 164

def move_records(params)
end

#new_record(params = {}) ⇒ Object

Returns a new record.



168
169
170
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 168

def new_record(params = {})
  nil
end

#primary_keyObject

Returns primary key name of the model



7
8
9
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 7

def primary_key
  "id"
end

#primary_key_attr?(a) ⇒ Boolean

Whether passed attribute config represents the primary key

Returns:

  • (Boolean)


12
13
14
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 12

def primary_key_attr?(a)
  a[:name].to_s == primary_key
end

#record_to_array(r, attrs) ⇒ Object

Transforms a record to an array of values according to the passed attributes attrs - array of attribute config hashes



191
192
193
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 191

def record_to_array(r, attrs)
  []
end

#record_to_hash(r, attrs) ⇒ Object

Transforms a record to a hash of values according to the passed attributes attrs - array of attribute config hashes



197
198
199
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 197

def record_to_hash(r, attrs)
  {}
end

#record_value_for_attribute(r, a, through_association = false) ⇒ Object

Fetches the value specified by an (association) attribute If through_association is true, get the value of the association by provided method, not the associated record's id E.g., author__name with through_association set to true may return “Vladimir Nabokov”, while with through_association set to false, it'll return author_id for the current record



215
216
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 215

def record_value_for_attribute(r, a, through_association = false)
end

#rootObject

Return root record for tree-like data



229
230
231
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 229

def root
  model.root
end

#save_record(record) ⇒ Object

give the data adapter the opportunity the set special options for saving, must return true on success



174
175
176
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 174

def save_record(record)
  record.save
end

#set_record_value_for_attribute(record, attr, value) ⇒ Object

Assigns new value to an (association) attribute in a given record role - role provided for mass assignment protection



220
221
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 220

def set_record_value_for_attribute(record, attr, value)
end

#virtual_attribute?(c) ⇒ Boolean

should return true if column is virtual

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


121
122
123
# File 'lib/netzke/basepack/data_adapters/abstract_adapter.rb', line 121

def virtual_attribute? c
  raise NotImplementedError
end