Class: Netfira::WebConnect::Model::Relation

Inherits:
Netfira::WebConnect::Model show all
Includes:
Events
Defined in:
lib/netfira/web_connect/model.rb,
lib/netfira/web_connect/model/relation.rb,
lib/netfira/web_connect/model/relation/events.rb

Defined Under Namespace

Modules: Events

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Netfira::WebConnect::Model

#dispatch_event, plural_name, single_name

Class Attribute Details

Returns the value of attribute related_classes.



9
10
11
# File 'lib/netfira/web_connect/model/relation.rb', line 9

def related_classes
  @related_classes
end

Class Method Details

.for(class_a, class_b) ⇒ Object



119
120
121
122
# File 'lib/netfira/web_connect/model/relation.rb', line 119

def for(class_a, class_b)
  name = [class_a, class_b].map{ |c| (c.is_a?(Class) ? c : c.class).name.demodulize }.sort.join 'To'
  Models.const_get name if Models.const_defined? name
end

.for!(*args) ⇒ Object



124
125
126
127
128
# File 'lib/netfira/web_connect/model/relation.rb', line 124

def for!(*args)
  self.for(*args).tap do |result|
    raise args.map { |c| (Class === c ? c : c.class).name.demodulize.pluralize }.join ' are not related to ' unless result
  end
end

.materialize(name_a, name_b) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/netfira/web_connect/model/relation.rb', line 11

def materialize(name_a, name_b)

  # The new class, e.g. Models::CategoryToProduct
  klass = Class.new(self)
  Models.const_set "#{name_a}To#{name_b}", klass

  # An array of related classes, e.g. [Models::Category, Models::Product]
  klass.related_classes = [name_a, name_b].map{ |n| Models.const_get n.camelize.singularize }

  # Sets up paranoia, including a with_deleted scope for consistency
  if Netfira::WebConnect.paranoia?
    klass.acts_as_paranoid
  else
    def klass.with_deleted; self end
  end

  # Enables finding by origin IDs, e.g.:
  # CategoryToProduct.find_by_origin_ids(category: 'fruit', product: 'apple', shop: 5)
  find_by_origin_ids = proc do |**args|

    # The table of the current class, e.g. nf_categories_to_products
    join_table = klass.arel_table

    # The scope we're building
    scope = klass

    # Include a shop in the scope if one is given
    shop = args[:shop]
    if shop
      shop = shop.id if Models::Shop === shop
      raise "`shop` must be an integer or an instance of #{Models::Shop.name}" unless Fixnum === shop
    end

    # Join each class and add where clauses
    klass.related_classes.each do |related_class|

      # The related table, e.g. nf_products
      related_table = related_class.arel_table

      # The argument expected to identify the relevant row of the table, e.g. :product
      origin_id_arg = related_class.single_name.to_sym

      # The origin ID to find in the table, e.g. 'apple'
      origin_id = args[origin_id_arg] or raise "Missing argument `#{origin_id_arg}`"

      # The column in which to seek the origin ID, e.g. :product_id
      origin_key = related_class.origin_key.to_sym

      # Join the related table, e.g.:
      # INNER JOIN "nf_products" ON "nf_categories_to_products"."product_id" = "nf_products"."id"
      scope = scope.joins join_table.join(related_table).on(join_table[origin_key].eq related_table[:id]).join_sql

      # Look for the origin ID, e.g.:
      # WHERE "nf_products"."product_id" = 'apple'
      scope = scope.where related_table[origin_key].eq origin_id

      # Limit to the given shop, e.g.:
      # AND "nf_products"."shop_id" = 5
      scope = scope.where related_table[:shop_id].eq shop if shop
    end

    # Only look for one record.
    scope.limit 1
  end
  klass.scope :find_by_origin_ids, find_by_origin_ids
end

.table_nameObject



111
112
113
114
115
116
117
# File 'lib/netfira/web_connect/model/relation.rb', line 111

def table_name
  @table_name ||= if self == Model::Relation
    Models::Table.table_name
  else
    Netfira::WebConnect.db_table_prefix(related_classes.map(&:plural_name).join '_to_').to_s
  end
end

Instance Method Details

#recordsObject



132
133
134
135
# File 'lib/netfira/web_connect/model/relation.rb', line 132

def records
  self.class.current_scope = nil if Netfira::WebConnect.paranoia? # Fixes a bug triggered by paranoia
  self.class.related_classes.map{ |klass| __send__ klass.single_name.to_sym }
end