Class: Netzke::Basepack::DataAdapters::DataMapperAdapter

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractAdapter

adapter_class, #first, inherited, #initialize, #new_record, #save_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/data_mapper_adapter.rb', line 3

def self.for_class?(model_class)
  model_class <= DataMapper::Resource
end

Instance Method Details

#class_for(assoc_name) ⇒ Object

Returns the model class for an association



158
159
160
# File 'lib/netzke/basepack/data_adapters/data_mapper_adapter.rb', line 158

def class_for assoc_name
  @model_class.send(assoc_name).model
end

#column_virtual?(c) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
107
108
109
# File 'lib/netzke/basepack/data_adapters/data_mapper_adapter.rb', line 99

def column_virtual? c
  assoc_name, assoc_method = c[:name].split '__'
  if assoc_method
    column_names=@model_class.send(assoc_name).model.column_names
    column_name=assoc_method
  else
    column_names=@model_class.column_names
    column_name=c[:name]
  end
  !column_names.include? column_name
end

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

Returns options for comboboxes in grids/forms



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/netzke/basepack/data_adapters/data_mapper_adapter.rb', line 112

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
      relation = @model_class.send(assoc_name).model

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

      if class_for(assoc_name).column_names.include?(assoc_method)
        # apply query
        relation = relation.all(assoc_method.to_sym.send(:like) => "%#{query}%") if query.present?
        relation.all.map{ |r| [r.id, r.send(assoc_method)] }
      else
        relation.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



65
66
67
68
69
70
71
72
# File 'lib/netzke/basepack/data_adapters/data_mapper_adapter.rb', line 65

def count_records(params, columns=[])
  # delete pagig related params, as this would break the query
  params=params.reject { |k, v|
    [:start, :limit, :page].include? k.to_sym
  }
  # this will NOT do a SELECT *, but a SELECT COUNT(*)
  get_records(params, columns).count
end

#destroy(ids) ⇒ Object



162
163
164
# File 'lib/netzke/basepack/data_adapters/data_mapper_adapter.rb', line 162

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

#destroy_allObject



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

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



188
189
190
# File 'lib/netzke/basepack/data_adapters/data_mapper_adapter.rb', line 188

def errors_array(record)
  record.errors.to_a.flatten
end

#find_record(id) ⇒ Object



166
167
168
# File 'lib/netzke/basepack/data_adapters/data_mapper_adapter.rb', line 166

def find_record(id)
  @model_class.get(id)
end

#foreign_key_for(assoc_name) ⇒ Object



153
154
155
# File 'lib/netzke/basepack/data_adapters/data_mapper_adapter.rb', line 153

def foreign_key_for assoc_name
  @model_class.relationships[assoc_name].child_key.first.name.to_s
end

#get_assoc_property_type(assoc_name, prop_name) ⇒ Object



88
89
90
91
92
# File 'lib/netzke/basepack/data_adapters/data_mapper_adapter.rb', line 88

def get_assoc_property_type assoc_name, prop_name
  assoc = @model_class.send(assoc_name)
  # prop_name could be a virtual column, check it first, return nil in this case
  assoc.respond_to?(prop_name) ? map_type(assoc.send(prop_name).property.class) : nil
end

#get_property_type(column) ⇒ Object

like get_assoc_property_type but for non-association columns



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

def get_property_type column
  map_type(column.class)
end

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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/netzke/basepack/data_adapters/data_mapper_adapter.rb', line 7

def get_records(params, columns=[])
  search_query = @model_class

  # used for specifying models to join (for ordering and column selection)
  links = []
  # join association models into query if they are specified in query
  # NOTE: AFAIK, in DataMapper there is no possibility to do
  # OUTER JOINs, and inner join is not correct if a foreign key is
  # nullable, so we don't join at all and instead rely on strategic
  # eager loading.

  #columns.each do |column|
    #if column[:name].index('__')
      #assoc, _ = column[:name].split('__')
      #link = @model_class.relationships[assoc.to_sym].inverse
      #links << link unless links.include? link
    #end
  #end

  # apply filter
  search_query = apply_column_filters search_query, params[:filter] if params[:filter]
  query_options = {}

  # apply sorting
  if params[:sort] && sort_params = params[:sort]
    order = []
    sort_params.each do |sort_param|
      assoc, method = sort_param["property"].split("__")
      dir = sort_param["direction"].downcase

      # if a sorting scope is set, call the scope with the given direction
      column = columns.detect { |c| c[:name] == sort_param["property"] }
      if column.try(:'has_key?', :sorting_scope)
        search_query = search_query.send(column[:sorting_scope].to_sym, dir.to_sym)
      else
        if method
          order << @model_class.send(assoc).send(method).send(dir)
          link = @model_class.relationships[assoc.to_sym].inverse
          links << link unless links.include? link
        else
          order << assoc.to_sym.send(dir)
        end
      end
    end
    query_options[:order] = order unless order.empty?
    query_options[:links] = links unless links.empty?
  end

  # apply paging
  query_options[:limit]=params[:limit] if params[:limit]
  query_options[:offset]=params[:start] if params[:start]

  # apply scope
  search_query = search_query.extend_with(params[:scope]) if params[:scope]

  search_query.all(query_options)
end

#hash_fk_modelObject

Build a hash of foreign keys and the associated model



171
172
173
174
175
176
177
178
# File 'lib/netzke/basepack/data_adapters/data_mapper_adapter.rb', line 171

def hash_fk_model
  @model_class.relationships.inject({}) do |foreign_keys, rel|
    if rel.kind_of? DataMapper::Associations::ManyToOne::Relationship
      foreign_keys[rel.child_key.first.name]=rel.parent_model.to_s.downcase.to_sym
      foreign_keys
    end
  end || {}
end

#lastObject

Needed for seed and tests



193
194
195
# File 'lib/netzke/basepack/data_adapters/data_mapper_adapter.rb', line 193

def last
  @model_class.last
end

#map_type(type) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/netzke/basepack/data_adapters/data_mapper_adapter.rb', line 74

def map_type type
  @typemap ||= {
    DataMapper::Property::Integer => :integer,
    DataMapper::Property::Serial => :integer,
    DataMapper::Property::Boolean => :boolean,
    DataMapper::Property::Date => :date,
    DataMapper::Property::DateTime => :datetime,
    DataMapper::Property::Time => :time,
    DataMapper::Property::String => :string,
    DataMapper::Property::Text => :text
  }
  @typemap[type]
end

#move_records(params) ⇒ Object



180
181
182
183
184
# File 'lib/netzke/basepack/data_adapters/data_mapper_adapter.rb', line 180

def move_records(params)
  @model_class.all(:id => params[:ids]).each_with_index do |item, index|
    item.move(:to => params[:new_index] + index)
  end
end