Class: Netzke::Basepack::DataAdapters::SequelAdapter

Inherits:
AbstractAdapter show all
Defined in:
lib/netzke/basepack/data_adapters/sequel_adapter.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractAdapter

adapter_class, #first, inherited, #initialize, #new_record

Constructor Details

This class inherits a constructor from Netzke::Basepack::DataAdapters::AbstractAdapter

Class Method Details

.for_class?(model_class) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/netzke/basepack/data_adapters/sequel_adapter.rb', line 3

def self.for_class?(model_class)
  model_class <= Sequel::Model
end

Instance Method Details

#class_for(assoc_name) ⇒ Object

Returns the model class for an association



87
88
89
# File 'lib/netzke/basepack/data_adapters/sequel_adapter.rb', line 87

def class_for assoc_name
  @model_class.association_reflection(assoc_name.to_sym)[:class_name].constantize
end

#column_virtual?(c) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
# File 'lib/netzke/basepack/data_adapters/sequel_adapter.rb', line 31

def column_virtual? c
  assoc, method = c[:name].split '__'
  if method
    !class_for(assoc.to_sym).columns.include? method.to_sym
  else
    !@model_class.columns.include? assoc.to_sym
  end
end

#combobox_options_for_column(column, method_options = {}) ⇒ Object

Returns options for comboboxes in grids/forms



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/netzke/basepack/data_adapters/sequel_adapter.rb', line 41

def combobox_options_for_column(column, method_options = {})
  query = method_options[:query]

  # First, check if we have options for this column defined in persistent storage
  options = column[:combobox_options] && column[:combobox_options].split("\n")
  if options
    query ? options.select{ |o| o.index(/^#{query}/) }.map{ |el| [el] } : options
  else
    assoc_name, assoc_method = column[:name].split '__'

    if assoc_name
      # Options for an asssociation attribute
      dataset = class_for(assoc_name)

      dataset = dataset.extend_with(method_options[:scope]) if method_options[:scope]

      if class_for(assoc_name).column_names.include?(assoc_method)
        # apply query
        dataset = dataset.where(assoc_method.to_sym.like("%#{query}%")) if query.present?
        dataset.all.map{ |r| [r.id, r.send(assoc_method)] }
      else
        dataset.all.map{ |r| [r.id, r.send(assoc_method)] }.select{ |id,value| value =~ /^#{query}/ }
      end
    else
      # Options for a non-association attribute
      res=@model_class.netzke_combo_options_for(column[:name], method_options)

      # ensure it is an array-in-array, as Ext will fail otherwise
      raise RuntimeError, "netzke_combo_options_for should return an Array" unless res.kind_of? Array
      return [[]] if res.empty?

      unless res.first.kind_of? Array
        res=res.map do |v|
          [v]
        end
      end
      return res
    end
  end
end

#count_records(params, columns = []) ⇒ Object



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

def count_records(params, columns=[])
  # dont pass columns, JOINs will be done as necessary for filters
  get_dataset(params, [], true).count
end

#destroy(ids) ⇒ Object



91
92
93
# File 'lib/netzke/basepack/data_adapters/sequel_adapter.rb', line 91

def destroy(ids)
  @model_class.where(:id => ids).destroy
end

#destroy_allObject

Needed for seed and tests



136
137
138
# File 'lib/netzke/basepack/data_adapters/sequel_adapter.rb', line 136

def destroy_all
  @model_class.destroy
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”]



122
123
124
125
126
127
128
# File 'lib/netzke/basepack/data_adapters/sequel_adapter.rb', line 122

def errors_array(record)
  record.errors.to_a.inject([]) do |errors, error|
    field, message = error
    errors << "#{record.class.human_attribute_name(field)} #{message.join ', '}"
    errors
  end
end

#find_record(id) ⇒ Object



95
96
97
# File 'lib/netzke/basepack/data_adapters/sequel_adapter.rb', line 95

def find_record(id)
  @model_class[id]
end

#foreign_key_for(assoc_name) ⇒ Object



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

def foreign_key_for assoc_name
  @model_class.association_reflection(assoc_name.to_sym)[:key].to_s
end

#get_assoc_property_type(assoc_name, prop_name) ⇒ Object



20
21
22
23
24
# File 'lib/netzke/basepack/data_adapters/sequel_adapter.rb', line 20

def get_assoc_property_type assoc_name, prop_name
  db_schema=class_for(assoc_name.to_sym).db_schema
  # return nil if prop_name not present in db schema (virtual column)
  db_schema[prop_name.to_sym] ? db_schema[prop_name.to_sym][:type] : nil
end

#get_property_type(column) ⇒ Object

like get_assoc_property_type but for non-association columns



27
28
29
# File 'lib/netzke/basepack/data_adapters/sequel_adapter.rb', line 27

def get_property_type column
  column[:type]
end

#get_records(params, columns = []) ⇒ Object



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

def get_records(params, columns=[])
  get_dataset(params, columns).all
end

#hash_fk_modelObject

Build a hash of foreign keys and the associated model



100
101
102
103
104
105
# File 'lib/netzke/basepack/data_adapters/sequel_adapter.rb', line 100

def hash_fk_model
  @model_class.all_association_reflections.inject({}) do |res, assoc|
    res[assoc[:key]] = assoc[:class_name].constantize.model_name.underscore.to_sym
    res
  end
end

#lastObject

Needed for seed and tests



131
132
133
# File 'lib/netzke/basepack/data_adapters/sequel_adapter.rb', line 131

def last
  @model_class.last
end

#map_type(type) ⇒ Object



16
17
18
# File 'lib/netzke/basepack/data_adapters/sequel_adapter.rb', line 16

def map_type type
  type
end

#move_records(params) ⇒ Object

TODO: is this possible with Sequel?



108
109
# File 'lib/netzke/basepack/data_adapters/sequel_adapter.rb', line 108

def move_records(params)
end

#save_record(record) ⇒ Object

give the data adapter the opportunity the set special options for saving



113
114
115
116
117
118
# File 'lib/netzke/basepack/data_adapters/sequel_adapter.rb', line 113

def save_record(record)
  # don't raise an error on saving. basepack will evaluate record.errors
  # to get validation errors
  record.raise_on_save_failure = false
  record.save
end