Class: Ooor::Relation
- Inherits:
-
Object
- Object
- Ooor::Relation
- Defined in:
- lib/ooor/relation.rb
Overview
Similar to Active Record Relation
Direct Known Subclasses
Instance Attribute Summary collapse
-
#count_field ⇒ Object
Returns the value of attribute count_field.
-
#create_with_value ⇒ Object
Returns the value of attribute create_with_value.
-
#eager_load_values ⇒ Object
Returns the value of attribute eager_load_values.
-
#from_value ⇒ Object
Returns the value of attribute from_value.
-
#group_values ⇒ Object
Returns the value of attribute group_values.
-
#having_values ⇒ Object
Returns the value of attribute having_values.
-
#includes_values ⇒ Object
Returns the value of attribute includes_values.
-
#joins_values ⇒ Object
Returns the value of attribute joins_values.
-
#klass ⇒ Object
(also: #model)
readonly
Returns the value of attribute klass.
-
#limit_value ⇒ Object
Returns the value of attribute limit_value.
-
#loaded ⇒ Object
(also: #loaded?)
readonly
Returns the value of attribute loaded.
-
#lock_value ⇒ Object
Returns the value of attribute lock_value.
-
#offset_value ⇒ Object
Returns the value of attribute offset_value.
-
#options ⇒ Object
Returns the value of attribute options.
-
#order_values ⇒ Object
Returns the value of attribute order_values.
-
#page_value ⇒ Object
Returns the value of attribute page_value.
-
#per_value ⇒ Object
Returns the value of attribute per_value.
-
#preload_values ⇒ Object
Returns the value of attribute preload_values.
-
#readonly_value ⇒ Object
Returns the value of attribute readonly_value.
-
#reorder_flag ⇒ Object
Returns the value of attribute reorder_flag.
-
#select_values ⇒ Object
Returns the value of attribute select_values.
-
#where_values ⇒ Object
Returns the value of attribute where_values.
Instance Method Summary collapse
-
#all(*args) ⇒ Object
A convenience wrapper for
find(:all, *args)
. - #apply_finder_options(options) ⇒ Object
-
#build_where(opts, other = []) ⇒ Object
TODO OpenERP domain is more than just the intersection of restrictions.
- #eager_loading? ⇒ Boolean
- #first(*args) ⇒ Object
-
#initialize(klass, options = {}) ⇒ Relation
constructor
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.
- #initialize_copy(other) ⇒ Object
- #inspect ⇒ Object
- #last(*args) ⇒ Object
-
#limit(value) ⇒ Object
def having(*args) relation = clone relation.having_values += build_where(*args) unless args.blank? relation end.
- #new(*args, &block) ⇒ Object (also: #build)
- #offset(value) ⇒ Object
- #order(*args) ⇒ Object
- #reload ⇒ Object
- #reset ⇒ Object
- #to_a ⇒ Object
- #where(opts, *rest) ⇒ Object
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
66 67 68 69 70 71 72 73 74 |
# File 'lib/ooor/relation.rb', line 66 def initialize(klass, ={}) @klass = klass @where_values = [] @loaded = false @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)
185 186 187 188 189 190 191 192 193 |
# File 'lib/ooor/relation.rb', line 185 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_field ⇒ Object
Returns the value of attribute count_field.
12 13 14 |
# File 'lib/ooor/relation.rb', line 12 def count_field @count_field end |
#create_with_value ⇒ Object
Returns the value of attribute create_with_value.
12 13 14 |
# File 'lib/ooor/relation.rb', line 12 def create_with_value @create_with_value end |
#eager_load_values ⇒ Object
Returns the value of attribute eager_load_values.
12 13 14 |
# File 'lib/ooor/relation.rb', line 12 def eager_load_values @eager_load_values end |
#from_value ⇒ Object
Returns the value of attribute from_value.
12 13 14 |
# File 'lib/ooor/relation.rb', line 12 def from_value @from_value end |
#group_values ⇒ Object
Returns the value of attribute group_values.
12 13 14 |
# File 'lib/ooor/relation.rb', line 12 def group_values @group_values end |
#having_values ⇒ Object
Returns the value of attribute having_values.
12 13 14 |
# File 'lib/ooor/relation.rb', line 12 def having_values @having_values end |
#includes_values ⇒ Object
Returns the value of attribute includes_values.
12 13 14 |
# File 'lib/ooor/relation.rb', line 12 def includes_values @includes_values end |
#joins_values ⇒ Object
Returns the value of attribute joins_values.
12 13 14 |
# File 'lib/ooor/relation.rb', line 12 def joins_values @joins_values end |
#klass ⇒ Object (readonly) Also known as: model
Returns the value of attribute klass.
11 12 13 |
# File 'lib/ooor/relation.rb', line 11 def klass @klass end |
#limit_value ⇒ Object
Returns the value of attribute limit_value.
12 13 14 |
# File 'lib/ooor/relation.rb', line 12 def limit_value @limit_value end |
#loaded ⇒ Object (readonly) Also known as: loaded?
Returns the value of attribute loaded.
11 12 13 |
# File 'lib/ooor/relation.rb', line 11 def loaded @loaded end |
#lock_value ⇒ Object
Returns the value of attribute lock_value.
12 13 14 |
# File 'lib/ooor/relation.rb', line 12 def lock_value @lock_value end |
#offset_value ⇒ Object
Returns the value of attribute offset_value.
12 13 14 |
# File 'lib/ooor/relation.rb', line 12 def offset_value @offset_value end |
#options ⇒ Object
Returns the value of attribute options.
12 13 14 |
# File 'lib/ooor/relation.rb', line 12 def @options end |
#order_values ⇒ Object
Returns the value of attribute order_values.
12 13 14 |
# File 'lib/ooor/relation.rb', line 12 def order_values @order_values end |
#page_value ⇒ Object
Returns the value of attribute page_value.
12 13 14 |
# File 'lib/ooor/relation.rb', line 12 def page_value @page_value end |
#per_value ⇒ Object
Returns the value of attribute per_value.
12 13 14 |
# File 'lib/ooor/relation.rb', line 12 def per_value @per_value end |
#preload_values ⇒ Object
Returns the value of attribute preload_values.
12 13 14 |
# File 'lib/ooor/relation.rb', line 12 def preload_values @preload_values end |
#readonly_value ⇒ Object
Returns the value of attribute readonly_value.
12 13 14 |
# File 'lib/ooor/relation.rb', line 12 def readonly_value @readonly_value end |
#reorder_flag ⇒ Object
Returns the value of attribute reorder_flag.
12 13 14 |
# File 'lib/ooor/relation.rb', line 12 def reorder_flag @reorder_flag end |
#select_values ⇒ Object
Returns the value of attribute select_values.
12 13 14 |
# File 'lib/ooor/relation.rb', line 12 def select_values @select_values end |
#where_values ⇒ Object
Returns the value of attribute where_values.
12 13 14 |
# File 'lib/ooor/relation.rb', line 12 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)
.
116 117 118 |
# File 'lib/ooor/relation.rb', line 116 def all(*args) args.any? ? (args.first).to_a : to_a end |
#apply_finder_options(options) ⇒ Object
100 101 102 103 104 |
# File 'lib/ooor/relation.rb', line 100 def () relation = clone relation. = #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
18 19 20 21 22 23 24 25 |
# File 'lib/ooor/relation.rb', line 18 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
149 150 151 |
# File 'lib/ooor/relation.rb', line 149 def eager_loading? false end |
#first(*args) ⇒ Object
120 121 122 |
# File 'lib/ooor/relation.rb', line 120 def first(*args) limit(1).order('id').all(*args).first end |
#initialize_copy(other) ⇒ Object
89 90 91 |
# File 'lib/ooor/relation.rb', line 89 def initialize_copy(other) reset end |
#inspect ⇒ Object
153 154 155 156 157 158 |
# File 'lib/ooor/relation.rb', line 153 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
124 125 126 |
# File 'lib/ooor/relation.rb', line 124 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
43 44 45 46 47 |
# File 'lib/ooor/relation.rb', line 43 def limit(value) relation = clone relation.limit_value = value relation end |
#new(*args, &block) ⇒ Object Also known as: build
76 77 78 79 |
# File 'lib/ooor/relation.rb', line 76 def new(*args, &block) #TODO inject current domain in *args @klass.new(*args, &block) end |
#offset(value) ⇒ Object
49 50 51 52 53 |
# File 'lib/ooor/relation.rb', line 49 def offset(value) relation = clone relation.offset_value = value relation end |
#order(*args) ⇒ Object
55 56 57 58 59 |
# File 'lib/ooor/relation.rb', line 55 def order(*args) relation = clone relation.order_values += args.flatten unless args.blank? || args[0] == false relation end |
#reload ⇒ Object
83 84 85 86 87 |
# File 'lib/ooor/relation.rb', line 83 def reload reset to_a # force reload self end |
#reset ⇒ Object
93 94 95 96 97 98 |
# File 'lib/ooor/relation.rb', line 93 def reset @first = @last = @to_sql = @order_clause = @scope_for_create = @arel = @loaded = nil @should_eager_load = @join_dependency = nil @records = [] self end |
#to_a ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/ooor/relation.rb', line 128 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
27 28 29 30 31 32 33 34 35 |
# File 'lib/ooor/relation.rb', line 27 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 |