Class: Urbix::ActsAsView::ViewRelations
- Inherits:
-
Object
- Object
- Urbix::ActsAsView::ViewRelations
- Defined in:
- lib/urbix/acts_as_view.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#needed_relations ⇒ Object
readonly
Returns the value of attribute needed_relations.
Instance Method Summary collapse
- #add(name, belongs_to_list) ⇒ Object
- #from_clause ⇒ Object
-
#initialize(model_klass) ⇒ ViewRelations
constructor
A new instance of ViewRelations.
- #join_clause ⇒ Object
- #select_clause ⇒ Object
Constructor Details
#initialize(model_klass) ⇒ ViewRelations
Returns a new instance of ViewRelations.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/urbix/acts_as_view.rb', line 11 def initialize(model_klass) @klass = model_klass @selects = [] # hash which associate tables to an array of belongs_to relation chain # for each belongs_to relations chain this class will genereate a table reference # in from_clause and a join condition in join_clause @needed_relations = {} # hash wich associate a belongs_to chain to a table_reference @table_ref = {} end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
9 10 11 |
# File 'lib/urbix/acts_as_view.rb', line 9 def klass @klass end |
#needed_relations ⇒ Object (readonly)
Returns the value of attribute needed_relations.
9 10 11 |
# File 'lib/urbix/acts_as_view.rb', line 9 def needed_relations @needed_relations end |
Instance Method Details
#add(name, belongs_to_list) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/urbix/acts_as_view.rb', line 22 def add(name, belongs_to_list) belongs_list = belongs_to_list.dup # 1. retrieve column name that is the last element from belongs_rel_list col_name = belongs_list.pop # 2. get table reference for the complete belongs_to relation chain # this action will also add the corresponding relation if needed table_ref = ref_bl_list(belongs_list) # 3. Build select clause for this relation @selects << "#{table_ref}.#{col_name} as #{name}" # 4. add all relations required by the belongs_to list until belongs_list.empty? ref_bl_list(belongs_list) belongs_list.pop end end |
#from_clause ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/urbix/acts_as_view.rb', line 54 def from_clause clause = [klass.table_name] @needed_relations.each do |ref_table,v| clause << ref_table i = 1 v[1..-1].each{ |bl| clause << "#{ref_table} #{ref_table}_#{i}" ; i+=1 } end clause.join(', ') end |
#join_clause ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/urbix/acts_as_view.rb', line 38 def join_clause clause = [] @needed_relations.each do |ref_table,rel_list| rel_list.each do |relation| reflection = relation.last fk = reflection.foreign_key pk = reflection.klass.primary_key bl_list = relation[0...-1] + [reflection.name] ref_table = @table_ref[relation[0...-1]] || klass.table_name bel_table = @table_ref[bl_list] clause << "#{ref_table}.#{fk}=#{bel_table}.#{pk}" end end clause.join(" and ") end |
#select_clause ⇒ Object
64 65 66 |
# File 'lib/urbix/acts_as_view.rb', line 64 def select_clause @selects.join(', ') end |