Class: ActiveFacts::Persistence::ForeignKey
- Inherits:
-
Object
- Object
- ActiveFacts::Persistence::ForeignKey
- Includes:
- Generate::Rails::Helpers
- Defined in:
- lib/activefacts/mapping/rails.rb,
lib/activefacts/generate/helpers/rails.rb,
lib/activefacts/persistence/foreignkey.rb
Instance Method Summary collapse
- #describe ⇒ Object
-
#following_references ⇒ Object
Which references are absorbed into the “to” table?.
-
#from ⇒ Object
What table (ObjectType) is the FK from?.
-
#from_columns ⇒ Object
What columns in the from table form the FK.
-
#from_name ⇒ Object
The from_name is the role name of the table with the FK, viewed from the other end When there are no precursor_references or following_references, it’s the jump_reference.from_names REVISIT: I’m still working out what to do with precursor_references and following_references.
-
#initialize(from, to, references, from_columns, to_columns) ⇒ ForeignKey
constructor
:nodoc:.
- #jump_reference ⇒ Object
-
#precursor_references ⇒ Object
Which references are absorbed into the “from” table?.
- #rails_from_association_name ⇒ Object
- #rails_to_association ⇒ Object
-
#references ⇒ Object
What reference created the FK?.
-
#to ⇒ Object
What table (ObjectType) is the FK to?.
-
#to_columns ⇒ Object
What columns in the to table form the identifier.
- #to_name ⇒ Object
- #verbalised_path ⇒ Object
Methods included from Generate::Rails::Helpers
#rails_class_name, #rails_plural_name, #rails_singular_name
Constructor Details
#initialize(from, to, references, from_columns, to_columns) ⇒ ForeignKey
:nodoc:
25 26 27 28 |
# File 'lib/activefacts/persistence/foreignkey.rb', line 25 def initialize(from, to, references, from_columns, to_columns) #:nodoc: @from, @to, @references, @from_columns, @to_columns = from, to, references, from_columns, to_columns end |
Instance Method Details
#describe ⇒ Object
30 31 32 |
# File 'lib/activefacts/persistence/foreignkey.rb', line 30 def describe "foreign key from #{from.name}(#{from_columns.map{|c| c.name}*', '}) to #{to.name}(#{to_columns.map{|c| c.name}*', '})" end |
#following_references ⇒ Object
Which references are absorbed into the “to” table?
51 52 53 54 55 |
# File 'lib/activefacts/persistence/foreignkey.rb', line 51 def following_references fk_jump = @references.detect(&:fk_jump) jump_index = @references.index(fk_jump) fk_jump != @references.last ? @references[jump_index+1..-1] : [] end |
#from ⇒ Object
What table (ObjectType) is the FK from?
11 |
# File 'lib/activefacts/persistence/foreignkey.rb', line 11 def from; @from; end |
#from_columns ⇒ Object
What columns in the from table form the FK
20 |
# File 'lib/activefacts/persistence/foreignkey.rb', line 20 def from_columns; @from_columns; end |
#from_name ⇒ Object
The from_name is the role name of the table with the FK, viewed from the other end When there are no precursor_references or following_references, it’s the jump_reference.from_names REVISIT: I’m still working out what to do with precursor_references and following_references
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/activefacts/persistence/foreignkey.rb', line 73 def from_name p = precursor_references f = following_references j = jump_reference # pluralise unless j.is_one_to_one # REVISIT: references[0].from_names is where the FK lives; but the object of interest may be an absorbed subclass which we should use here instead: # REVISIT: Should crunch superclasses in subtype traversals # REVISIT: Need to add "_as_rolename" where rolename is not to.name [ @references[0].from_names, (p.empty? && f.empty? ? [] : ['via'] + p.map{|r| r.to_names}.flatten + f.map{|r| r.from_names}.flatten) ] end |
#jump_reference ⇒ Object
57 58 59 |
# File 'lib/activefacts/persistence/foreignkey.rb', line 57 def jump_reference @references.detect(&:fk_jump) end |
#precursor_references ⇒ Object
Which references are absorbed into the “from” table?
44 45 46 47 48 |
# File 'lib/activefacts/persistence/foreignkey.rb', line 44 def precursor_references fk_jump = @references.detect(&:fk_jump) jump_index = @references.index(fk_jump) @references[0, jump_index] end |
#rails_from_association_name ⇒ Object
92 93 94 |
# File 'lib/activefacts/mapping/rails.rb', line 92 def rails_from_association_name Persistence::rails_singular_name(to_name.join('_')) end |
#rails_to_association ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/activefacts/mapping/rails.rb', line 96 def rails_to_association jump = jump_reference if jump.is_one_to_one [ "has_one", Persistence::rails_singular_name(from_name)] else [ "has_many", Persistence::rails_plural_name(from_name)] end end |
#references ⇒ Object
What reference created the FK?
17 |
# File 'lib/activefacts/persistence/foreignkey.rb', line 17 def references; @references; end |
#to ⇒ Object
What table (ObjectType) is the FK to?
14 |
# File 'lib/activefacts/persistence/foreignkey.rb', line 14 def to; @to; end |
#to_columns ⇒ Object
What columns in the to table form the identifier
23 |
# File 'lib/activefacts/persistence/foreignkey.rb', line 23 def to_columns; @to_columns; end |
#to_name ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/activefacts/persistence/foreignkey.rb', line 61 def to_name p = precursor_references f = following_references j = jump_reference @references.last.to_names + (p.empty? && f.empty? ? [] : ['via'] + p.map{|r| r.to_names}.flatten + f.map{|r| r.from_names}.flatten) end |
#verbalised_path ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/activefacts/persistence/foreignkey.rb', line 34 def verbalised_path # REVISIT: This should be a proper join path verbalisation: references.map do |r| (r.fact_type.entity_type ? r.fact_type.entity_type.name + ' (in which ' : '') + r.fact_type.default_reading + (r.fact_type.entity_type ? ')' : '') end * ' and ' end |