counter_culture 1.0.0 → 1.1.0

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: ff55da448ed08ad68f53c9ae8fee1e814b99315e
4
- data.tar.gz: e3f2a61342ab909fabd3a0d0188a288bcbde0879
3
+ metadata.gz: adde693fbc8a3158f577294d8c824bd4635b9a2e
4
+ data.tar.gz: 1841858ea2173cd8582d1f90a01c72e2c50afa59
5
5
  SHA512:
6
- metadata.gz: 9ddc79c3684fbbb45d411327ca4c2a28e8b782eecc212b202312b145f43f6e0e200f7b4858edde90b9eb85434ba1dc0ea89eaa2773a26e3e03af24506e440ac6
7
- data.tar.gz: 94e34148fead1c2e426b3133a7772da18e37183009b62280e92f3348fc9e980b762fbd3fe14bde0b76b8e2ee529cffd5df229f0e374b08167fb72a5753622aa8
6
+ metadata.gz: e60214e311c99e3d9ee493fb605293388eab2880211dcb7eea0ebded7606a5b1a622664de9a461e4a9c3d57fc9089816cb713af7c371066af94be4945acb1b96
7
+ data.tar.gz: b5b0ef3c373f7017e69e781898b690de491721fd5847024a119c3bd6f73fb1bed02dc344ff1630730f6c67ecfbc0c4a68b21f3ab4c22c647496a09b2e3c9f090
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 1.1.0 (December 23, 2016)
2
+
3
+ Improvements:
4
+ - Support for has_one associations
5
+
1
6
  ## 1.0.0 (November 15, 2016)
2
7
 
3
8
  Breaking changes:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.0
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: counter_culture 1.0.0 ruby lib
5
+ # stub: counter_culture 1.1.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "counter_culture"
9
- s.version = "1.0.0"
9
+ s.version = "1.1.0"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Magnus von Koeller"]
14
- s.date = "2016-11-15"
14
+ s.date = "2016-12-23"
15
15
  s.description = "counter_culture provides turbo-charged counter caches that are kept up-to-date not just on create and destroy, that support multiple levels of indirection through relationships, allow dynamic column names and that avoid deadlocks by updating in the after_commit callback."
16
16
  s.email = "magnus@vonkoeller.de"
17
17
  s.extra_rdoc_files = [
@@ -45,18 +45,22 @@ Gem::Specification.new do |s|
45
45
  "spec/models/categ.rb",
46
46
  "spec/models/category.rb",
47
47
  "spec/models/company.rb",
48
+ "spec/models/company_access_level.rb",
48
49
  "spec/models/conditional_dependent.rb",
49
50
  "spec/models/conditional_main.rb",
50
51
  "spec/models/has_string_id.rb",
51
52
  "spec/models/industry.rb",
53
+ "spec/models/person.rb",
52
54
  "spec/models/post.rb",
53
55
  "spec/models/post_comment.rb",
54
56
  "spec/models/product.rb",
57
+ "spec/models/recruiter.rb",
55
58
  "spec/models/review.rb",
56
59
  "spec/models/simple_dependent.rb",
57
60
  "spec/models/simple_main.rb",
58
61
  "spec/models/simple_review.rb",
59
62
  "spec/models/subcateg.rb",
63
+ "spec/models/transaction.rb",
60
64
  "spec/models/twitter_review.rb",
61
65
  "spec/models/user.rb",
62
66
  "spec/rails_app/.gitignore",
@@ -124,8 +124,13 @@ module CounterCulture
124
124
  else
125
125
  join_table_name = reflect.active_record.table_name
126
126
  end
127
+ if reflect.has_one?
128
+ on_sql = "#{reflect.table_name}.#{reflect.foreign_key} = #{join_table_name}.#{reflect.association_primary_key}"
129
+ else
130
+ on_sql = "#{reflect.table_name}.#{reflect.association_primary_key} = #{join_table_name}.#{reflect.foreign_key}"
131
+ end
127
132
  # join with alias to avoid ambiguous table name with self-referential models:
128
- joins_sql = "LEFT JOIN #{reflect.active_record.table_name} AS #{join_table_name} ON #{reflect.table_name}.#{reflect.association_primary_key} = #{join_table_name}.#{reflect.foreign_key}"
133
+ joins_sql = "LEFT JOIN #{reflect.active_record.table_name} AS #{join_table_name} ON #{on_sql}"
129
134
  # adds 'type' condition to JOIN clause if the current model is a child in a Single Table Inheritance
130
135
  joins_sql = "#{joins_sql} AND #{reflect.active_record.table_name}.type IN ('#{model.name}')" if reflect.active_record.column_names.include?('type') && !model.descends_from_active_record?
131
136
  joins_sql
@@ -1,6 +1,8 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  require 'models/company'
4
+ require 'models/company_access_level'
5
+ require 'models/recruiter'
4
6
  require 'models/industry'
5
7
  require 'models/product'
6
8
  require 'models/review'
@@ -19,6 +21,8 @@ require 'models/categ'
19
21
  require 'models/subcateg'
20
22
  require 'models/another_post'
21
23
  require 'models/another_post_comment'
24
+ require 'models/person'
25
+ require 'models/transaction'
22
26
 
23
27
  require 'database_cleaner'
24
28
  DatabaseCleaner.strategy = :deletion
@@ -1501,5 +1505,51 @@ describe "CounterCulture" do
1501
1505
  fixed.length.should == 1
1502
1506
  company.reload.children_count.should == 1
1503
1507
  end
1508
+
1509
+ it "works with a has_one association" do
1510
+ company = Company.create!
1511
+ company.recruiters << Recruiter.create!
1512
+ company.reload.recruiters_count.should == 1
1513
+
1514
+ company.update_columns(recruiters_count: 2)
1515
+
1516
+ Recruiter.counter_culture_fix_counts
1517
+ company.reload.recruiters_count.should == 1
1518
+ end
1519
+ end
1520
+
1521
+ describe "dynamic column names with totaling instead of counting" do
1522
+ it "should correctly sum up the values" do
1523
+ person = Person.create!
1524
+
1525
+ earning_transaction = Transaction.create(monetary_value: 10, person: person)
1526
+
1527
+ person.reload
1528
+ person.money_earned_total.should == 10
1529
+
1530
+ spending_transaction = Transaction.create(monetary_value: -20, person: person)
1531
+ person.reload
1532
+ person.money_spent_total.should == -20
1533
+ end
1534
+
1535
+ it "should show the correct changes when changes are present" do
1536
+ person = Person.create(id:100)
1537
+
1538
+ earning_transaction = Transaction.create(monetary_value: 10, person: person)
1539
+ spending_transaction = Transaction.create(monetary_value: -20, person: person)
1540
+
1541
+ # Overwrite the values for the person so they are incorrect
1542
+ person.reload
1543
+ person.money_earned_total = 0
1544
+ person.money_spent_total = 0
1545
+ person.save
1546
+
1547
+ fixed = Transaction.counter_culture_fix_counts
1548
+ fixed.length.should == 2
1549
+ fixed.should == [
1550
+ {:entity=>"Person", :id=>100, :what=>"money_earned_total", :wrong=>0, :right=>10},
1551
+ {:entity=>"Person", :id=>100, :what=>"money_spent_total", :wrong=>0, :right=>-20}
1552
+ ]
1553
+ end
1504
1554
  end
1505
1555
  end
@@ -6,6 +6,8 @@ class Company < ActiveRecord::Base
6
6
  belongs_to :parent, :class_name => 'Company', :foreign_key => 'parent_id'
7
7
  has_many :children, :class_name => 'Company', :foreign_key => 'parent_id'
8
8
 
9
- counter_culture :parent, :column_name => :children_count
9
+ has_many :company_access_levels
10
+ has_many :recruiters, through: :company_access_levels
10
11
 
12
+ counter_culture :parent, :column_name => :children_count
11
13
  end
@@ -0,0 +1,4 @@
1
+ class CompanyAccessLevel < ActiveRecord::Base
2
+ belongs_to :company
3
+ belongs_to :recruiter
4
+ end
@@ -0,0 +1,4 @@
1
+ class Person < ActiveRecord::Base
2
+
3
+ has_many :transactions
4
+ end
@@ -0,0 +1,5 @@
1
+ class Recruiter < ActiveRecord::Base
2
+ has_one :company_access_level
3
+
4
+ counter_culture [:company_access_level, :company]
5
+ end
@@ -0,0 +1,15 @@
1
+ class Transaction < ActiveRecord::Base
2
+ belongs_to :person
3
+
4
+ counter_culture [:person],
5
+ :column_name => proc {|model| model.earns_money? ? 'money_earned_total' : 'money_spent_total' },
6
+ :column_names => {
7
+ ["transactions.monetary_value > 0"] => 'money_earned_total',
8
+ ["transactions.monetary_value <= 0"] => 'money_spent_total'
9
+ },
10
+ :delta_column => 'monetary_value'
11
+
12
+ def earns_money?
13
+ monetary_value > 0
14
+ end
15
+ end
data/spec/schema.rb CHANGED
@@ -24,10 +24,16 @@ ActiveRecord::Schema.define(:version => 20120522160158) do
24
24
  t.integer "review_approvals_count", :default => 0, :null => false
25
25
  t.integer "parent_id"
26
26
  t.integer "children_count", :default => 0, :null => false
27
+ t.integer "recruiters_count", :default => 0, :null => false
27
28
  t.datetime "created_at"
28
29
  t.datetime "updated_at"
29
30
  end
30
31
 
32
+ create_table "company_access_levels", :force => true do |t|
33
+ t.integer "company_id"
34
+ t.integer "recruiter_id"
35
+ end
36
+
31
37
  create_table "industries", :force => true do |t|
32
38
  t.string "name"
33
39
  t.integer "reviews_count", :default => 0, :null => false
@@ -63,6 +69,11 @@ ActiveRecord::Schema.define(:version => 20120522160158) do
63
69
  t.datetime "updated_at"
64
70
  end
65
71
 
72
+ create_table "recruiters", :force => true do |t|
73
+ t.datetime "created_at"
74
+ t.datetime "updated_at"
75
+ end
76
+
66
77
  create_table "users", :force => true do |t|
67
78
  t.string "name"
68
79
  t.integer "company_id"
@@ -164,4 +175,13 @@ ActiveRecord::Schema.define(:version => 20120522160158) do
164
175
  t.datetime "updated_at", :null => false
165
176
  end
166
177
 
178
+ create_table "people", :force => true do |t|
179
+ t.integer "money_earned_total", :null => false, :default => 0
180
+ t.integer "money_spent_total", :null => false, :default => 0
181
+ end
182
+
183
+ create_table "transactions", :force => true do |t|
184
+ t.integer "person_id", :null => false
185
+ t.integer "monetary_value", :null => false
186
+ end
167
187
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: counter_culture
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Magnus von Koeller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-15 00:00:00.000000000 Z
11
+ date: 2016-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: after_commit_action
@@ -187,18 +187,22 @@ files:
187
187
  - spec/models/categ.rb
188
188
  - spec/models/category.rb
189
189
  - spec/models/company.rb
190
+ - spec/models/company_access_level.rb
190
191
  - spec/models/conditional_dependent.rb
191
192
  - spec/models/conditional_main.rb
192
193
  - spec/models/has_string_id.rb
193
194
  - spec/models/industry.rb
195
+ - spec/models/person.rb
194
196
  - spec/models/post.rb
195
197
  - spec/models/post_comment.rb
196
198
  - spec/models/product.rb
199
+ - spec/models/recruiter.rb
197
200
  - spec/models/review.rb
198
201
  - spec/models/simple_dependent.rb
199
202
  - spec/models/simple_main.rb
200
203
  - spec/models/simple_review.rb
201
204
  - spec/models/subcateg.rb
205
+ - spec/models/transaction.rb
202
206
  - spec/models/twitter_review.rb
203
207
  - spec/models/user.rb
204
208
  - spec/rails_app/.gitignore