Class: Ooor::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/ooor/relation.rb

Overview

Direct Known Subclasses

Associations::CollectionProxy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, options = {}) ⇒ Relation

def count(column_name = nil, options = {}) #TODO possible to implement?

  column_name, options = nil, column_name if column_name.is_a?(Hash)
  calculate(:count, column_name, options)
end


73
74
75
76
77
78
79
80
81
# File 'lib/ooor/relation.rb', line 73

def initialize(klass, options={})
  @klass = klass
  @where_values = []
  @loaded = false
  @options = options
  @count_field = false
  @offset_value = 0
  @order_values = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)



193
194
195
196
197
198
199
200
201
# File 'lib/ooor/relation.rb', line 193

def method_missing(method, *args, &block)
  if Array.method_defined?(method)
    to_a.send(method, *args, &block)
  elsif @klass.respond_to?(method)
    @klass.send(method, *args, &block)
  else
    @klass.rpc_execute(method.to_s, to_a.map {|record| record.id}, *args)
  end
end

Instance Attribute Details

#count_fieldObject

Returns the value of attribute count_field.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def count_field
  @count_field
end

#create_with_valueObject

Returns the value of attribute create_with_value.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def create_with_value
  @create_with_value
end

#eager_load_valuesObject

Returns the value of attribute eager_load_values.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def eager_load_values
  @eager_load_values
end

#from_valueObject

Returns the value of attribute from_value.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def from_value
  @from_value
end

#group_valuesObject

Returns the value of attribute group_values.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def group_values
  @group_values
end

#having_valuesObject

Returns the value of attribute having_values.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def having_values
  @having_values
end

#includes_valuesObject

Returns the value of attribute includes_values.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def includes_values
  @includes_values
end

#joins_valuesObject

Returns the value of attribute joins_values.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def joins_values
  @joins_values
end

#klassObject (readonly) Also known as: model

Returns the value of attribute klass.



12
13
14
# File 'lib/ooor/relation.rb', line 12

def klass
  @klass
end

#limit_valueObject

Returns the value of attribute limit_value.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def limit_value
  @limit_value
end

#loadedObject (readonly) Also known as: loaded?

Returns the value of attribute loaded.



12
13
14
# File 'lib/ooor/relation.rb', line 12

def loaded
  @loaded
end

#lock_valueObject

Returns the value of attribute lock_value.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def lock_value
  @lock_value
end

#offset_valueObject

Returns the value of attribute offset_value.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def offset_value
  @offset_value
end

#optionsObject

Returns the value of attribute options.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def options
  @options
end

#order_valuesObject

Returns the value of attribute order_values.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def order_values
  @order_values
end

#page_valueObject

Returns the value of attribute page_value.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def page_value
  @page_value
end

#per_valueObject

Returns the value of attribute per_value.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def per_value
  @per_value
end

#preload_valuesObject

Returns the value of attribute preload_values.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def preload_values
  @preload_values
end

#readonly_valueObject

Returns the value of attribute readonly_value.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def readonly_value
  @readonly_value
end

#reorder_flagObject

Returns the value of attribute reorder_flag.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def reorder_flag
  @reorder_flag
end

#select_valuesObject

Returns the value of attribute select_values.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def select_values
  @select_values
end

#where_valuesObject

Returns the value of attribute where_values.



13
14
15
# File 'lib/ooor/relation.rb', line 13

def where_values
  @where_values
end

Instance Method Details

#all(*args) ⇒ Object

A convenience wrapper for find(:all, *args). You can pass in all the same arguments to this method as you can to find(:all).



123
124
125
# File 'lib/ooor/relation.rb', line 123

def all(*args)
  args.any? ? apply_finder_options(args.first).to_a : to_a
end

#apply_finder_options(options) ⇒ Object



107
108
109
110
111
# File 'lib/ooor/relation.rb', line 107

def apply_finder_options(options)
  relation = clone
  relation.options = options #TODO this may be too simplified for chainability, merge smartly instead?
  relation
end

#build_where(opts, other = []) ⇒ Object

TODO OpenERP domain is more than just the intersection of restrictions



19
20
21
22
23
24
25
26
# File 'lib/ooor/relation.rb', line 19

def build_where(opts, other = [])#TODO OpenERP domain is more than just the intersection of restrictions
  case opts
  when Array || '|' || '&'
    [opts]
  when Hash
    opts.keys.map {|key|["#{key}", "=", opts[key]]}
  end
end

#eager_loading?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/ooor/relation.rb', line 156

def eager_loading?
  false
end

#first(*args) ⇒ Object



127
128
129
# File 'lib/ooor/relation.rb', line 127

def first(*args)
  limit(1).order('id').all(*args).first
end

#includes(*values) ⇒ Object



62
63
64
65
66
# File 'lib/ooor/relation.rb', line 62

def includes(*values)
  relation = clone
  relation.includes_values = values
  relation
end

#initialize_copy(other) ⇒ Object



96
97
98
# File 'lib/ooor/relation.rb', line 96

def initialize_copy(other)
  reset
end

#inspectObject



160
161
162
163
164
165
# File 'lib/ooor/relation.rb', line 160

def inspect
  entries = to_a.take([limit_value, 11].compact.min).map!(&:inspect)
  entries[10] = '...' if entries.size == 11

  "#<#{self.class.name} [#{entries.join(', ')}]>"
end

#last(*args) ⇒ Object



131
132
133
# File 'lib/ooor/relation.rb', line 131

def last(*args)
  limit(1).order('id DESC').all(*args).first
end

#limit(value) ⇒ Object

def having(*args)

  relation = clone
  relation.having_values += build_where(*args) unless args.blank?
  relation
end


44
45
46
47
48
# File 'lib/ooor/relation.rb', line 44

def limit(value)
  relation = clone
  relation.limit_value = value
  relation
end

#new(*args, &block) ⇒ Object Also known as: build



83
84
85
86
# File 'lib/ooor/relation.rb', line 83

def new(*args, &block)
  #TODO inject current domain in *args
  @klass.new(*args, &block)
end

#offset(value) ⇒ Object



50
51
52
53
54
# File 'lib/ooor/relation.rb', line 50

def offset(value)
  relation = clone
  relation.offset_value = value
  relation
end

#order(*args) ⇒ Object



56
57
58
59
60
# File 'lib/ooor/relation.rb', line 56

def order(*args)
  relation = clone
  relation.order_values += args.flatten unless args.blank? || args[0] == false
  relation
end

#reloadObject



90
91
92
93
94
# File 'lib/ooor/relation.rb', line 90

def reload
  reset
  to_a # force reload
  self
end

#resetObject



100
101
102
103
104
105
# File 'lib/ooor/relation.rb', line 100

def reset
  @first = @last = @to_sql = @order_clause = @scope_for_create = @arel = @loaded = nil
  @should_eager_load = @join_dependency = nil
  @records = []
  self
end

#to_aObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/ooor/relation.rb', line 135

def to_a
  if loaded?
    @records
  else
    if @order_values.empty?
      search_order = false
    else
      search_order = @order_values.join(", ")
    end

    if @options && @options[:name_search]
      name_search = @klass.name_search(@options[:name_search], where_values, 'ilike', @options[:context], @limit_value)
      @records = name_search.map do |tuple|
        @klass.new({name: tuple[1]}, []).tap { |r| r.id = tuple[0] } #TODO load the fields optionally
      end
    else
      load_records_page(search_order)
    end
  end
end

#where(opts, *rest) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/ooor/relation.rb', line 28

def where(opts, *rest)
  relation = clone
  if opts.is_a?(Array) && opts.any? {|e| e.is_a? Array}
    relation.where_values = opts
  else
    relation.where_values += build_where(opts, rest) unless opts.blank?
  end
  relation
end