web-connect 0.4.0 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a8bff92c7d13988bc68cf2aa9623c9621624319a
4
- data.tar.gz: 303b28ea1214ce4c065527f0b5ab1efda2d2268c
3
+ metadata.gz: 435761d473e6207771accd7586c0c8b6003e7073
4
+ data.tar.gz: a17604aba5f073120cc6c3d5abe496f92e14a4b8
5
5
  SHA512:
6
- metadata.gz: 7136d9aa509a71ff9d8208e5d463746d4f5d99c5a30ff1718ccd6427e7cbc0c95fc4432bf6f597b647f441d34b2070d77e875c430e635d9da114a506dd6882fb
7
- data.tar.gz: 4b9672b379f9330b7537c35b88980de3405ac7d5294e3150af2814ac9049b8e9fb02a6f11b117cf533fc9ee6d20f85bc02b9b4d0ff99a46593e8e46077c41a41
6
+ metadata.gz: faa5af85ed89bebf95e56a031d1c15d59a44fb8becba703058e2c559cc85fa637799a62f14f369bbab378521c4e7f9c524c7506b9b56e34eed1547a75b96009c
7
+ data.tar.gz: de81cfb5722537b64e7e704b86f937e85904a4f7a37be182af4560307c309c5855cb30be65c0ca4bf7a5e932eff9a2cec883153685960ad90e01d4d9b6dd6349
@@ -44,8 +44,7 @@ class Alpha < Netfira::WebConnect::Migration
44
44
  t.boolean :show_on_website
45
45
  end
46
46
 
47
- create_record_table :categories, with_l10n: true do |t|
48
- t.integer :parent_id
47
+ create_record_table :categories, with_l10n: true, tree: true do |t|
49
48
  t.localized_string :name
50
49
  end
51
50
 
@@ -23,6 +23,10 @@ module Netfira::WebConnect
23
23
  t.string options[:origin_key] || :"#{table_name.to_s.singularize}_id", index: true unless options[:writable]
24
24
  yield t
25
25
  t.references :shop, index: true
26
+ if options[:tree]
27
+ t.string :parent_id
28
+ t.index [:shop_id, :parent_id]
29
+ end
26
30
  if options[:sendable]
27
31
  t.binary :guid, index: true
28
32
  t.integer :delivery_status, default: 0, limit: 3, index: true
@@ -2,11 +2,21 @@ module Netfira::WebConnect
2
2
  module Model::Record::Tree
3
3
 
4
4
  def children
5
- self.class.where(parent_id: id)
5
+ (origin_id.nil? || origin_id.empty?) ? [] : self.class.where(shop_id: shop_id, parent_id: origin_id)
6
6
  end
7
7
 
8
8
  def parent
9
- @parent ||= parent_id && self.class.find_by(id: parent_id)
9
+ @parent ||= parent_id && !parent_id.empty? && self.class.find_by(shop_id: shop_id, origin_key => parent_id)
10
+ end
11
+
12
+ def parent_id=(value)
13
+ if Fixnum === value
14
+ @parent = self.class.find(value)
15
+ value = @parent.origin_id
16
+ elsif value != parent_id
17
+ @parent = nil
18
+ end
19
+ self[:parent_id] = value
10
20
  end
11
21
 
12
22
  end
@@ -109,6 +109,9 @@ module Netfira::WebConnect
109
109
  schema = self.class.schema
110
110
  if schema.localize.include? key
111
111
  set_translated_string key, value
112
+ elsif key == :parent_id
113
+ value = value.to_s
114
+ write_attribute key, value.empty? ? nil : value
112
115
  else
113
116
  type = schema.columns[key]
114
117
  return false unless type
@@ -9,14 +9,70 @@ module Netfira::WebConnect
9
9
  attr_reader :related_classes
10
10
 
11
11
  def materialize(name_a, name_b)
12
+
13
+ # The new class, e.g. Models::CategoryToProduct
12
14
  klass = Class.new(self)
13
15
  Models.const_set "#{name_a}To#{name_b}", klass
16
+
17
+ # An array of related classes, e.g. [Models::Category, Models::Product]
14
18
  klass.related_classes = [name_a, name_b].map{ |n| Models.const_get n.camelize.singularize }
19
+
20
+ # Sets up paranoia, including a with_deleted scope for consistency
15
21
  if Netfira::WebConnect.paranoia?
16
22
  klass.acts_as_paranoid
17
23
  else
18
24
  def klass.with_deleted; self end
19
25
  end
26
+
27
+ # Enables finding by origin IDs, e.g.:
28
+ # CategoryToProduct.find_by_origin_ids(category: 'fruit', product: 'apple', shop: 5)
29
+ find_by_origin_ids = proc do |**args|
30
+
31
+ # The table of the current class, e.g. nf_categories_to_products
32
+ join_table = klass.arel_table
33
+
34
+ # The scope we're building
35
+ scope = klass
36
+
37
+ # Include a shop in the scope if one is given
38
+ shop = args[:shop]
39
+ if shop
40
+ shop = shop.id if Models::Shop === shop
41
+ raise "`shop` must be an integer or an instance of #{Models::Shop.name}" unless Fixnum === shop
42
+ end
43
+
44
+ # Join each class and add where clauses
45
+ klass.related_classes.each do |related_class|
46
+
47
+ # The related table, e.g. nf_products
48
+ related_table = related_class.arel_table
49
+
50
+ # The argument expected to identify the relevant row of the table, e.g. :product
51
+ origin_id_arg = related_class.single_name.to_sym
52
+
53
+ # The origin ID to find in the table, e.g. 'apple'
54
+ origin_id = args[origin_id_arg] or raise "Missing argument `#{origin_id_arg}`"
55
+
56
+ # The column in which to seek the origin ID, e.g. :product_id
57
+ origin_key = related_class.origin_key.to_sym
58
+
59
+ # Join the related table, e.g.:
60
+ # INNER JOIN "nf_products" ON "nf_categories_to_products"."product_id" = "nf_products"."id"
61
+ scope = scope.joins join_table.join(related_table).on(join_table[origin_key].eq related_table[:id]).join_sql
62
+
63
+ # Look for the origin ID, e.g.:
64
+ # WHERE "nf_products"."product_id" = 'apple'
65
+ scope = scope.where related_table[origin_key].eq origin_id
66
+
67
+ # Limit to the given shop, e.g.:
68
+ # AND "nf_products"."shop_id" = 5
69
+ scope = scope.where related_table[:shop_id].eq shop if shop
70
+ end
71
+
72
+ # Only look for one record.
73
+ scope.limit 1
74
+ end
75
+ klass.scope :find_by_origin_ids, find_by_origin_ids
20
76
  end
21
77
 
22
78
  def related_classes=(classes)
@@ -65,6 +121,12 @@ module Netfira::WebConnect
65
121
  Models.const_get name if Models.const_defined? name
66
122
  end
67
123
 
124
+ def for!(*args)
125
+ self.for(*args).tap do |result|
126
+ raise args.map { |c| (Class === c ? c : c.class).name.demodulize.pluralize }.join ' are not related to ' unless result
127
+ end
128
+ end
129
+
68
130
  end
69
131
 
70
132
  def records
@@ -17,6 +17,16 @@ module Netfira::WebConnect
17
17
  settings[:locale] || Netfira::WebConnect.system_locale
18
18
  end
19
19
 
20
+ def find_relation_by_origin_ids(**args)
21
+ raise ArgumentError, "Provide exactly 2 origin IDs, e.g. :product => 'abc'" unless args.length == 2
22
+ classes = args.keys.map do |type|
23
+ name = type.to_s.camelize.to_sym
24
+ Models.const_get(name).tap { |k| raise "#{type} isn't a valid record type. Use singular snake-case, e.g. :product" unless k < Model::Record }
25
+ end
26
+ relation_class = Model::Relation.for!(*classes)
27
+ relation_class.with_deleted.find_by_origin_ids(args.merge shop: self).first
28
+ end
29
+
20
30
  end
21
31
  end
22
32
 
@@ -1,6 +1,6 @@
1
1
  module Netfira
2
2
  module WebConnect
3
- VERSION = '0.4.0'
3
+ VERSION = '0.4.2'
4
4
  PLATFORM_AND_VERSION = 'Rack/' << VERSION
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: web-connect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Neil E. Pearson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-08-03 00:00:00.000000000 Z
12
+ date: 2014-08-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord