composite_primary_keys 11.0.0.beta4 → 11.0.0.rc1

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
  SHA256:
3
- metadata.gz: a82bde494ba8ade055be42d3e57d7d6f4eb31aebe5417e5e7c92590963943cab
4
- data.tar.gz: 28805dfbc66e70f35452d852a7f2a5f20248ec64a6798d844505c6a2aec7fc73
3
+ metadata.gz: bfbfea33787da9b0c727a2f72624aa10cab7f1fd7d00ff925cf6332bbb7eab5e
4
+ data.tar.gz: d357900c434c2b9a9574ecfbf616298197b7c1351391c874dea4a878e61b4beb
5
5
  SHA512:
6
- metadata.gz: 2e67fdc2a3846cc17ed323ffcbdc3d4eb2c2da3c898436f667ac071a3f6f1abc25983f77dd68798055a6a9365556d36c8631798f9bfed71cafe1a6ab3156e001
7
- data.tar.gz: facd88fcfacbc76cf14d0ca0c6f9c9ecce5d5ef39856f32eb40ed20d91e9e8b48bc40829bbcf2013c95249f689a0c32e189aba054cd580a2ec74b6075fc71908
6
+ metadata.gz: 9fb6e448aae23c666782fa4a2f70225cd8834207f3b1eb853f342cf0f65d3d5c07ecadba8ae8a79f4b1e1830e2ee67596ad247e27c7c17abbc5343bbb07896b2
7
+ data.tar.gz: d7707078d862451d42f5c98b4fc2625b846beb478bcd5e91b8ab830d77e02dca083f8994e1ff681b3c35262687cbfbe0dbfb174161cef5ab2dcd4c8c7755b9af
@@ -1,3 +1,8 @@
1
+ == 11.0.0.rc1 (2018-02-25)
2
+
3
+ * Update to ActiveRecord 5.2.rc1 (Charlie Savage)
4
+ * Fix uniquness check (Charlie Savage)
5
+
1
6
  == 11.0.0.beta4 (2018-01-08)
2
7
 
3
8
  * Try again on Calculations#execute_simple_calculation for ActiveRecord 5.2.beta2 (Charlie Savage)
@@ -26,7 +26,7 @@ $:.unshift(File.dirname(__FILE__)) unless
26
26
 
27
27
  unless defined?(ActiveRecord)
28
28
  require 'rubygems'
29
- gem 'activerecord', '~> 5.2.0.beta2'
29
+ gem 'activerecord', '~> 5.2.0.rc1'
30
30
  require 'active_record'
31
31
  end
32
32
 
@@ -68,6 +68,8 @@ require 'active_record/relation/finder_methods'
68
68
  require 'active_record/relation/query_methods'
69
69
  require 'active_record/relation/predicate_builder/association_query_value'
70
70
 
71
+ require 'active_record/validations/uniqueness'
72
+
71
73
  # CPK files
72
74
  require 'composite_primary_keys/persistence'
73
75
  require 'composite_primary_keys/base'
@@ -108,7 +110,12 @@ require 'composite_primary_keys/relation/finder_methods'
108
110
  require 'composite_primary_keys/relation/predicate_builder/association_query_value'
109
111
  require 'composite_primary_keys/relation/query_methods'
110
112
 
113
+ require 'composite_primary_keys/validations/uniqueness'
114
+
111
115
  require 'composite_primary_keys/composite_relation'
112
116
 
113
117
  require 'composite_primary_keys/arel/to_sql'
114
- require 'composite_primary_keys/arel/sqlserver'
118
+
119
+ # SQL Servers Support - uncomment these lines
120
+ #require 'activerecord-sqlserver-adapter/arel/visitors/sqlserver'
121
+ #require 'composite_primary_keys/arel/sqlserver'
@@ -1,18 +1,18 @@
1
1
  module CompositePrimaryKeys
2
2
  module CollectionAssociation
3
3
  def ids_writer(ids)
4
- pk_type = reflection.association_primary_key_type
4
+ primary_key = reflection.association_primary_key
5
+ pk_type = klass.type_for_attribute(primary_key)
5
6
  ids = Array(ids).reject(&:blank?)
6
7
  ids.map! { |i| pk_type.cast(i) }
7
8
 
8
- # CPK
9
- if reflection.association_primary_key.is_a?(Array)
9
+ # CPK-
10
+ if primary_key.is_a?(Array)
10
11
  predicate = CompositePrimaryKeys::Predicates.cpk_in_predicate(klass.arel_table, reflection.association_primary_key, ids)
11
12
  records = klass.where(predicate).index_by do |r|
12
13
  reflection.association_primary_key.map{ |k| r.send(k) }
13
14
  end.values_at(*ids)
14
15
  else
15
- primary_key = reflection.association_primary_key
16
16
  records = klass.where(primary_key => ids).index_by do |r|
17
17
  r.public_send(primary_key)
18
18
  end.values_at(*ids).compact
@@ -30,22 +30,22 @@ module ActiveRecord
30
30
  end
31
31
  end
32
32
 
33
- # def relation_for_destroy
34
- # # CPK
35
- # if self.composite?
36
- # relation = self.class.unscoped
37
- #
38
- # Array(self.class.primary_key).each do |key|
39
- # column = self.class.arel_table[key]
40
- # value = self[key]
41
- # relation = relation.where(column.eq(value))
42
- # end
43
- #
44
- # relation
45
- # else
46
- # self.class.unscoped.where(self.class.primary_key => id)
47
- # end
48
- # end
33
+ def _relation_for_itself
34
+ # CPK
35
+ if self.composite?
36
+ relation = self.class.unscoped
37
+
38
+ Array(self.class.primary_key).each do |key|
39
+ column = self.class.arel_table[key]
40
+ value = self[key]
41
+ relation = relation.where(column.eq(value))
42
+ end
43
+
44
+ relation
45
+ else
46
+ self.class.unscoped.where(self.class.primary_key => id)
47
+ end
48
+ end
49
49
 
50
50
  def touch(*names, time: nil)
51
51
  raise ActiveRecordError, "cannot touch on a new record object" unless persisted?
@@ -1,8 +1,8 @@
1
1
  module ActiveRecord
2
2
  class Relation
3
3
  alias :initialize_without_cpk :initialize
4
- def initialize(klass, table, predicate_builder, values = {})
5
- initialize_without_cpk(klass, table, predicate_builder, values)
4
+ def initialize(klass, table: klass.arel_table, predicate_builder: klass.predicate_builder, values: {})
5
+ initialize_without_cpk(klass, table: table, predicate_builder: predicate_builder, values: values)
6
6
  add_cpk_support if klass && klass.composite?
7
7
  end
8
8
 
@@ -68,12 +68,12 @@ module ActiveRecord
68
68
  stmt.from(table)
69
69
 
70
70
  # CPK
71
- if joins_values.any? && @klass.composite?
71
+ if has_join_values? && @klass.composite?
72
72
  arel_attributes = Array(primary_key).map do |key|
73
73
  arel_attribute(key)
74
74
  end.to_composite_keys
75
75
  @klass.connection.join_to_delete(stmt, arel, arel_attributes)
76
- elsif has_join_values?
76
+ elsif has_join_values? || has_limit_or_offset?
77
77
  @klass.connection.join_to_delete(stmt, arel, arel_attribute(primary_key))
78
78
  else
79
79
  stmt.wheres = arel.constraints
@@ -58,11 +58,9 @@ module CompositePrimaryKeys
58
58
  relation = relation.where(conditions)
59
59
  end
60
60
  when Array, Hash
61
- relation = relation.where(conditions)
61
+ relation.where!(conditions)
62
62
  else
63
- unless conditions == :none
64
- relation = relation.where(primary_key => conditions)
65
- end
63
+ relation.where!(primary_key => conditions) unless conditions == :none
66
64
  end
67
65
 
68
66
  relation
@@ -4,11 +4,13 @@ module ActiveRecord
4
4
  def queries
5
5
  # CPK
6
6
  if associated_table.association_join_foreign_key.is_a?(Array)
7
- result = associated_table.association_join_foreign_key.zip(ids).reduce(Hash.new) do |hash, pair|
8
- hash[pair.first.to_s] = pair.last
9
- hash
7
+ if ids.is_a?(ActiveRecord::Relation)
8
+ ids.map do |id|
9
+ id.ids_hash
10
+ end
11
+ else
12
+ [associated_table.association_join_foreign_key.zip(ids).to_h]
10
13
  end
11
- [result]
12
14
  else
13
15
  [associated_table.association_join_foreign_key.to_s => ids]
14
16
  end
@@ -0,0 +1,32 @@
1
+ module ActiveRecord
2
+ module Validations
3
+ class UniquenessValidator
4
+ def validate_each(record, attribute, value)
5
+ finder_class = find_finder_class_for(record)
6
+ value = map_enum_attribute(finder_class, attribute, value)
7
+
8
+ relation = build_relation(finder_class, attribute, value)
9
+ if record.persisted?
10
+ # CPK
11
+ if finder_class.primary_key.is_a?(Array)
12
+ predicate = finder_class.cpk_id_predicate(finder_class.arel_table, finder_class.primary_key, record.id_in_database || record.id)
13
+ relation = relation.where.not(predicate)
14
+ elsif finder_class.primary_key
15
+ relation = relation.where.not(finder_class.primary_key => record.id_in_database || record.id)
16
+ else
17
+ raise UnknownPrimaryKey.new(finder_class, "Can not validate uniqueness for persisted record without primary key.")
18
+ end
19
+ end
20
+ relation = scope_relation(record, relation)
21
+ relation = relation.merge(options[:conditions]) if options[:conditions]
22
+
23
+ if relation.exists?
24
+ error_options = options.except(:case_sensitive, :scope, :conditions)
25
+ error_options[:value] = value
26
+
27
+ record.errors.add(attribute, :taken, error_options)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -3,6 +3,6 @@ module CompositePrimaryKeys
3
3
  MAJOR = 11
4
4
  MINOR = 0
5
5
  TINY = 0
6
- STRING = [MAJOR, MINOR, TINY, 'beta4'].join('.')
6
+ STRING = [MAJOR, MINOR, TINY, 'rc1'].join('.')
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: composite_primary_keys
3
3
  version: !ruby/object:Gem::Version
4
- version: 11.0.0.beta4
4
+ version: 11.0.0.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charlie Savage
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-08 00:00:00.000000000 Z
11
+ date: 2018-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -128,6 +128,7 @@ files:
128
128
  - lib/composite_primary_keys/relation/query_methods.rb
129
129
  - lib/composite_primary_keys/relation/where_clause.rb
130
130
  - lib/composite_primary_keys/sanitization.rb
131
+ - lib/composite_primary_keys/validations/uniqueness.rb
131
132
  - lib/composite_primary_keys/version.rb
132
133
  - scripts/console.rb
133
134
  - scripts/txt2html