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
67 68 69 70 71 72 73 74 75 |
# File 'lib/ooor/relation.rb', line 67 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)
186 187 188 189 190 191 192 193 194 |
# File 'lib/ooor/relation.rb', line 186 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.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def count_field @count_field end |
#create_with_value ⇒ Object
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_values ⇒ Object
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_value ⇒ Object
Returns the value of attribute from_value.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def from_value @from_value end |
#group_values ⇒ Object
Returns the value of attribute group_values.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def group_values @group_values end |
#having_values ⇒ Object
Returns the value of attribute having_values.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def having_values @having_values end |
#includes_values ⇒ Object
Returns the value of attribute includes_values.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def includes_values @includes_values end |
#joins_values ⇒ Object
Returns the value of attribute joins_values.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def joins_values @joins_values end |
#klass ⇒ Object (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_value ⇒ Object
Returns the value of attribute limit_value.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def limit_value @limit_value end |
#loaded ⇒ Object (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_value ⇒ Object
Returns the value of attribute lock_value.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def lock_value @lock_value end |
#offset_value ⇒ Object
Returns the value of attribute offset_value.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def offset_value @offset_value end |
#options ⇒ Object
Returns the value of attribute options.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def @options end |
#order_values ⇒ Object
Returns the value of attribute order_values.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def order_values @order_values end |
#page_value ⇒ Object
Returns the value of attribute page_value.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def page_value @page_value end |
#per_value ⇒ Object
Returns the value of attribute per_value.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def per_value @per_value end |
#preload_values ⇒ Object
Returns the value of attribute preload_values.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def preload_values @preload_values end |
#readonly_value ⇒ Object
Returns the value of attribute readonly_value.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def readonly_value @readonly_value end |
#reorder_flag ⇒ Object
Returns the value of attribute reorder_flag.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def reorder_flag @reorder_flag end |
#select_values ⇒ Object
Returns the value of attribute select_values.
13 14 15 |
# File 'lib/ooor/relation.rb', line 13 def select_values @select_values end |
#where_values ⇒ Object
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)
.
117 118 119 |
# File 'lib/ooor/relation.rb', line 117 def all(*args) args.any? ? (args.first).to_a : to_a end |
#apply_finder_options(options) ⇒ Object
101 102 103 104 105 |
# File 'lib/ooor/relation.rb', line 101 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
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
150 151 152 |
# File 'lib/ooor/relation.rb', line 150 def eager_loading? false end |
#first(*args) ⇒ Object
121 122 123 |
# File 'lib/ooor/relation.rb', line 121 def first(*args) limit(1).order('id').all(*args).first end |
#initialize_copy(other) ⇒ Object
90 91 92 |
# File 'lib/ooor/relation.rb', line 90 def initialize_copy(other) reset end |
#inspect ⇒ Object
154 155 156 157 158 159 |
# File 'lib/ooor/relation.rb', line 154 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
125 126 127 |
# File 'lib/ooor/relation.rb', line 125 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
77 78 79 80 |
# File 'lib/ooor/relation.rb', line 77 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 |
#reload ⇒ Object
84 85 86 87 88 |
# File 'lib/ooor/relation.rb', line 84 def reload reset to_a # force reload self end |
#reset ⇒ Object
94 95 96 97 98 99 |
# File 'lib/ooor/relation.rb', line 94 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
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/ooor/relation.rb', line 129 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 |