notches 0.6.0 → 0.7.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: ed673601115b4a08c8c6f46ac1ba576217d16d5a
4
- data.tar.gz: 65eb69efd2001ce4f544e70f6e5db92fd5f73781
3
+ metadata.gz: ece48dfc0a167d9cf7a227b28b9cb3c175da32d4
4
+ data.tar.gz: b0d3cc05b279bcda628d1d4efaf82ba67631ee8e
5
5
  SHA512:
6
- metadata.gz: 887a3b48a25567423d9b1bb38c4f72f93e7824eb2c391db65ad7decebb6b2f5cb854ef817733fd00ce6a13c30962140edaac39e29ccddebbdaca50158d35e6de
7
- data.tar.gz: 01eb20fe4cba9d8a82f7b4c48d7cd316377c13e1182e0d40061c8c42d6ee6d222ccd74e95f1c9f10b6432b212845bf1707e24c8ff2d3576fa27ac0ea83670d07
6
+ metadata.gz: ac4177237dfb759d2c001a94bf8967806ccf72ba4ced6ac596d774d6baac75b51bca94168141c6b85d259395a592efcc6f99d93994ac062a9573dd180ac45a3d
7
+ data.tar.gz: 0f83367e1b5ce4f2f8372e140a87bbc6c039c9fe061213d7d7331bccf113dff1b1242222f10e6bdb54241ad5d4eb8b595630eca8e7e92ce65b812fba80c699bd
data/README.md CHANGED
@@ -59,19 +59,25 @@ To get a better idea of how Notches is setup check out the
59
59
  Recording events
60
60
  ----------------
61
61
 
62
- To record events make the following call, the scope is optional.
62
+ To record events make the following call:
63
63
 
64
- Notches::Event.log(name: 'An important event', scope: 'A scope')
64
+ Notches::Event.log(name: 'An event')
65
+
66
+ Events can have one or two optional scopes:
67
+
68
+ Notches::Event.log(name: 'An event', scope: 'A person')
69
+ Notches::Event.log(name: 'An event', scope: ['A person', 'An object'])
65
70
 
66
71
  Counting events
67
72
  -------------
68
73
 
69
74
  For a name:
70
75
 
71
- Notches::Event.joins(:name).where('name like ?', 'An important event').count
76
+ Notches::Event.joins(:name).where('name = ?', 'An event').count
72
77
 
73
78
  For a name and scope:
74
79
 
75
80
  Notches::Event.joins(:name, :scope).where(
76
- 'name like = ? and scope like ?', 'An important event', 'A scope'
81
+ 'name = ? and scopes.primary = ? and scopes.secondary = ?',
82
+ 'An event', 'A person', 'An object'
77
83
  ).count
@@ -2,21 +2,22 @@ class Notches::Event < ActiveRecord::Base
2
2
  self.table_name = "notches_events"
3
3
 
4
4
  validates :name, presence: true
5
+ validates :scope, presence: true
5
6
 
6
7
  validates_associated :name, :scope
7
8
 
8
9
  belongs_to :date, foreign_key: :notches_date_id
9
- belongs_to :name, foreign_key: :notches_name_id, :class_name => Name.to_s
10
- belongs_to :scope, foreign_key: :notches_scope_id, :class_name => Scope.to_s
10
+ belongs_to :name, foreign_key: :notches_name_id, :class_name => Notches::Name.to_s
11
+ belongs_to :scope, foreign_key: :notches_scope_id, :class_name => Notches::Scope.to_s
11
12
  belongs_to :time, foreign_key: :notches_time_id
12
13
 
13
14
  def self.log(attributes)
14
15
  event = self.new
15
16
  event.transaction do
16
17
  Rails.logger.info("[Notches] Tracking #{attributes.inspect} at #{Date.today} #{Time.now}")
17
- now = Time.zone.now
18
+ now = Time.zone.now
18
19
  event.name = Notches::Name.find_or_create_by(name: attributes[:name])
19
- event.scope = Notches::Scope.find_or_create_by(scope: attributes[:scope]) if attributes[:scope].present?
20
+ event.scope = Notches::Scope.find_or_create_by(scope_attributes_for(attributes[:scope]))
20
21
  event.date = Notches::Date.find_or_create_by(date: now.to_date)
21
22
  event.time = Notches::Time.find_or_create_by_time(now)
22
23
 
@@ -28,4 +29,12 @@ class Notches::Event < ActiveRecord::Base
28
29
  end
29
30
  event
30
31
  end
32
+
33
+ def self.scope_attributes_for(scope)
34
+ scope = [scope] if !scope.is_a?(Array)
35
+ {
36
+ primary: scope.first.to_s,
37
+ secondary: scope.second.to_s
38
+ }
39
+ end
31
40
  end
@@ -4,5 +4,19 @@ class Notches::Scope < ActiveRecord::Base
4
4
  has_many :events, class_name: Notches::Event.to_s,
5
5
  foreign_key: "notches_scope_id"
6
6
 
7
- validates :scope, presence: true
7
+ validate :no_nulls
8
+ validate :no_secondary_scope_without_primary
9
+
10
+ private
11
+
12
+ def no_nulls
13
+ errors[:primary] << "can't be null" if primary.nil?
14
+ errors[:secondary] << "can't be null" if secondary.nil?
15
+ end
16
+
17
+ def no_secondary_scope_without_primary
18
+ if secondary.present? && primary.blank?
19
+ errors[:secondary] << "is invalid"
20
+ end
21
+ end
8
22
  end
@@ -0,0 +1,13 @@
1
+ class AddSecondaryToNotchesScopes < ActiveRecord::Migration
2
+ def up
3
+ rename_column :notches_scopes, :scope, :primary
4
+ add_column :notches_scopes, :secondary, :string
5
+ change_column_null :notches_events, :notches_scope_id, false
6
+ end
7
+
8
+ def down
9
+ change_column_null :notches_events, :notches_scope_id, true
10
+ remove_column :notches_scopes, :secondary
11
+ rename_column :notches_scopes, :primary, :scope
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module Notches
2
- VERSION = '0.6.0'
2
+ VERSION = '0.7.0'
3
3
  end
@@ -4,8 +4,8 @@ describe Notches::Event do
4
4
  describe "validations" do
5
5
  let(:event) do
6
6
  Notches::Event.new(
7
- name: Notches::Name.new(name: 'An important event'),
8
- scope: Notches::Scope.new(scope: 'An important person')
7
+ name: Notches::Name.new(name: 'An event'),
8
+ scope: Notches::Scope.new(primary: 'A person', secondary: '')
9
9
  )
10
10
  end
11
11
 
@@ -19,10 +19,10 @@ describe Notches::Event do
19
19
  expect(event.errors[:name]).to eq(["can't be blank"])
20
20
  end
21
21
 
22
- it "does not require a scope" do
22
+ it "requires a scope" do
23
23
  event.scope = nil
24
24
  event.valid?
25
- expect(event).to be_valid
25
+ expect(event.errors[:scope]).to eq(["can't be blank"])
26
26
  end
27
27
 
28
28
  it "does not validate if its Name's name is blank" do
@@ -31,72 +31,101 @@ describe Notches::Event do
31
31
  expect(event.errors[:name]).to eq(["is invalid"])
32
32
  end
33
33
 
34
- it "does not validate if it has a Scope but it's invalid" do
35
- event.scope.scope = nil
34
+ it "does not validate if it has a scope but it's value is nil" do
35
+ event.scope.primary = nil
36
+ event.valid?
37
+ expect(event.errors[:scope]).to eq(["is invalid"])
38
+ end
39
+
40
+ it "does not validate if it has a secondary scope but no primary scope" do
41
+ event.scope.primary = nil
42
+ event.scope.secondary = 'Secondary scope'
36
43
  event.valid?
37
44
  expect(event.errors[:scope]).to eq(["is invalid"])
38
45
  end
39
46
  end
40
47
 
41
48
  describe ".log" do
49
+ let(:now) { Time.zone.now }
50
+ let(:existing_event) { Notches::Event.log(name: "An event", scope: "A person") }
42
51
 
43
- context "with valid attributes" do
44
- let(:now) { Time.zone.now }
45
- before do
46
- allow(Time).to receive_message_chain(:zone, :now).and_return(now)
47
- end
52
+ before do
53
+ allow(Time).to receive_message_chain(:zone, :now).and_return(now)
54
+ end
48
55
 
56
+ context "with valid attributes" do
49
57
  it "creates a event for today and current time with those attributes" do
50
- Notches::Event.log(name: "An important event")
58
+ Notches::Event.log(name: "An event")
51
59
  event = Notches::Event.first
52
60
  expect(event).to be_present
53
- expect(event.name.name).to eq("An important event")
54
- expect(event.scope).to be_nil
61
+ expect(event.name.name).to eq("An event")
62
+ expect(event.scope.primary).to eq("")
63
+ expect(event.scope.secondary).to eq("")
55
64
  expect(event.date.date).to eq(now.to_date)
56
65
  expect(event.time.time.strftime('%H:%M:%S')).to eq(now.strftime('%H:%M:%S'))
57
66
  end
58
67
  end
59
68
 
60
- let(:existing_event) {
61
- Notches::Event.log(
62
- name: "An important event",
63
- scope: "An important person"
64
- )
65
- }
66
-
67
69
  it "does not create a new Name record if there's a record for that Name already" do
68
- event = Notches::Event.log(name: "An important event")
70
+ event = Notches::Event.log(name: "An event")
69
71
  expect(event.name.id).to eq(existing_event.name.id)
70
72
  end
71
73
 
72
74
  it "does not create a new Scope record if there's a record for that Scope already" do
73
- event = Notches::Event.log(
74
- name: "An important event",
75
- scope: "An important person"
76
- )
75
+ event = Notches::Event.log(name: "An event", scope: "A person")
77
76
  expect(event.scope.id).to eq(existing_event.scope.id)
78
77
  end
79
78
 
80
79
  it "does not create a new Date record there's a record for that Date already" do
81
- event = Notches::Event.log(name: "Another important event")
80
+ event = Notches::Event.log(name: "Another event")
82
81
  expect(event.date.id).to eq(existing_event.date.id)
83
82
  end
84
83
 
85
84
  it "does not create a new Time record if there's a record for that Time already even if date was different" do
86
- existing_event.time.update_attributes!(time: Time.parse('2016-08-03 14:15:59 UTC'))
87
- allow(Time).to receive_message_chain(:zone, :now).and_return(Time.parse('2016-08-04 14:15:59 UTC'))
88
- event = Notches::Event.log(name: "Another important event")
85
+ event = Notches::Event.log(name: "Another event")
89
86
  expect(event.time.id).to eq(existing_event.time.id)
90
87
  end
91
88
 
92
89
  context "a event with the same name, scope, date and time exists already" do
93
90
  it "does not log the event" do
94
- allow(Time).to receive_message_chain(:zone, :now).and_return(existing_event.time.time)
91
+ event_attributes = {
92
+ name: "An event",
93
+ scope: "A person"
94
+ }
95
+ Notches::Event.log(event_attributes)
96
+
97
+ expect {
98
+ Notches::Event.log(event_attributes)
99
+ }.to_not change { Notches::Event.count }
100
+ end
101
+ end
102
+
103
+ context "logging an event with a primary and secondary scope" do
104
+ it do
105
+ event = Notches::Event.log(
106
+ name: "An event",
107
+ scope: ["A person", "An object"]
108
+ )
109
+
110
+ expect(event).to be_persisted
111
+ expect(event.name.name).to eq("An event")
112
+ expect(event.scope.primary).to eq("A person")
113
+ expect(event.scope.secondary).to eq("An object")
114
+ expect(event.date.date).to eq(now.to_date)
115
+ expect(event.time.time.strftime('%H:%M:%S')).to eq(now.strftime('%H:%M:%S'))
116
+ end
117
+ end
118
+
119
+ context "logging an event with a primary and secondary scope when a event with same name, scope, date and time exists already" do
120
+ it "does not log the event" do
121
+ event_attributes = {
122
+ name: "An event",
123
+ scope: ["A person", "An object"]
124
+ }
125
+ Notches::Event.log(event_attributes)
126
+
95
127
  expect {
96
- event = Notches::Event.log(
97
- name: "An important event",
98
- scope: "An important person"
99
- )
128
+ Notches::Event.log(event_attributes)
100
129
  }.to_not change { Notches::Event.count }
101
130
  end
102
131
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notches
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cillian O'Ruanaidh
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-08-04 00:00:00.000000000 Z
12
+ date: 2016-08-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -100,6 +100,7 @@ files:
100
100
  - db/migrate/20120910093652_add_index_to_user_agents.rb
101
101
  - db/migrate/20130918115023_create_notches_user_sessions.rb
102
102
  - db/migrate/20160804111320_create_notches_events.rb
103
+ - db/migrate/20160815100820_add_secondary_to_notches_scopes.rb
103
104
  - lib/notches.rb
104
105
  - lib/notches/engine.rb
105
106
  - lib/notches/version.rb