Class: Terrestrial::Configurations::ConventionalAssociationConfiguration

Inherits:
Object
  • Object
show all
Defined in:
lib/terrestrial/configurations/conventional_association_configuration.rb

Constant Summary collapse

DEFAULT =
Module.new

Instance Method Summary collapse

Constructor Details

#initialize(inflector, datastore, mapping_name, mappings) ⇒ ConventionalAssociationConfiguration

Returns a new instance of ConventionalAssociationConfiguration.



13
14
15
16
17
18
# File 'lib/terrestrial/configurations/conventional_association_configuration.rb', line 13

def initialize(inflector, datastore, mapping_name, mappings)
  @inflector = inflector
  @datastore = datastore
  @target_mapping = mappings.fetch(mapping_name)
  @mappings = mappings
end

Instance Method Details

#belongs_to(association_name, key: DEFAULT, foreign_key: DEFAULT, mapping_name: DEFAULT) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/terrestrial/configurations/conventional_association_configuration.rb', line 51

def belongs_to(association_name, key: DEFAULT, foreign_key: DEFAULT, mapping_name: DEFAULT)
  defaults = {
    key: :id,
    foreign_key: [association_name, "_id"].join.to_sym,
    mapping_name: pluralize(association_name).to_sym,
  }

  specified = {
    mapping_name: mapping_name,
    foreign_key: foreign_key,
    key: key,
  }.reject { |_k,v|
    v == DEFAULT
  }

  config = defaults.merge(specified)

  associated_mapping_name = config.fetch(:mapping_name)
  associated_mapping = mappings.fetch(associated_mapping_name)

  belongs_to_mapper(**config)
end

#has_many(association_name, key: DEFAULT, foreign_key: DEFAULT, mapping_name: DEFAULT, order_fields: DEFAULT, order_direction: DEFAULT) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/terrestrial/configurations/conventional_association_configuration.rb', line 25

def has_many(association_name, key: DEFAULT, foreign_key: DEFAULT, mapping_name: DEFAULT, order_fields: DEFAULT, order_direction: DEFAULT)
  defaults = {
    mapping_name: association_name,
    foreign_key: [singular_name, "_id"].join.to_sym,
    key: :id,
    order_fields: [],
    order_direction: "ASC",
  }

  specified = {
    mapping_name: mapping_name,
    foreign_key: foreign_key,
    key: key,
    order_fields: order_fields,
    order_direction: order_direction,
  }.reject { |_k,v|
    v == DEFAULT
  }

  config = defaults.merge(specified)
  associated_mapping_name = config.fetch(:mapping_name)
  associated_mapping = mappings.fetch(associated_mapping_name)

  has_many_mapper(**config)
end

#has_many_through(association_name, key: DEFAULT, foreign_key: DEFAULT, mapping_name: DEFAULT, through_table_name: DEFAULT, association_key: DEFAULT, association_foreign_key: DEFAULT, order_fields: DEFAULT, order_direction: DEFAULT) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/terrestrial/configurations/conventional_association_configuration.rb', line 74

def has_many_through(association_name, key: DEFAULT, foreign_key: DEFAULT, mapping_name: DEFAULT, through_table_name: DEFAULT, association_key: DEFAULT, association_foreign_key: DEFAULT, order_fields: DEFAULT, order_direction: DEFAULT)
  # TODO: join_dataset as mutually exclusive option with join_table_name
  defaults = {
    mapping_name: association_name,
    key: :id,
    association_key: :id,
    foreign_key: [singular_name, "_id"].join.to_sym,
    association_foreign_key: [singularize(association_name), "_id"].join.to_sym,
    order_fields: [],
    order_direction: "ASC",
  }

  specified = {
    mapping_name: mapping_name,
    key: key,
    association_key: association_key,
    foreign_key: foreign_key,
    association_foreign_key: association_foreign_key,
    order_fields: order_fields,
    order_direction: order_direction,
  }.reject { |_k,v|
    v == DEFAULT
  }

  config = defaults.merge(specified)

  associated_mapping = mappings.fetch(config.fetch(:mapping_name))
  default_through_table_name = [associated_mapping.name, target_mapping.name].sort.join("_to_").to_sym

  if through_table_name == DEFAULT
    through_table_name = default_through_table_name
  end

  join_mapping = create_virtual_mapping(
    default_mapping_name: default_through_table_name,
    namespace: through_table_name,
    primary_key: [config[:foreign_key], config[:association_foreign_key]],
  )

  mappings[join_mapping.name] = join_mapping

  join_dataset = datastore[through_table_name.to_sym]

  config = config.merge(
    join_mapping_name: join_mapping.name,
    join_dataset: join_dataset,
  )

  has_many_through_mapper(**config)
end