notches 0.6.0 → 0.7.1

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
- SHA1:
3
- metadata.gz: ed673601115b4a08c8c6f46ac1ba576217d16d5a
4
- data.tar.gz: 65eb69efd2001ce4f544e70f6e5db92fd5f73781
2
+ SHA256:
3
+ metadata.gz: 586425ba4443bad8f9b073dedd839f73f48321e086421ed084137385b6faeaba
4
+ data.tar.gz: 9892fd375c4cf59bd677a35f7193312fd90411a26d8ca58581ee579fb2674524
5
5
  SHA512:
6
- metadata.gz: 887a3b48a25567423d9b1bb38c4f72f93e7824eb2c391db65ad7decebb6b2f5cb854ef817733fd00ce6a13c30962140edaac39e29ccddebbdaca50158d35e6de
7
- data.tar.gz: 01eb20fe4cba9d8a82f7b4c48d7cd316377c13e1182e0d40061c8c42d6ee6d222ccd74e95f1c9f10b6432b212845bf1707e24c8ff2d3576fa27ac0ea83670d07
6
+ metadata.gz: d23197a84edea814e445c9e0e10ee933ed6fbd15b4b5b7b75e7d8534efb82ec73614acf9bb9be37fc109b313990d943e6482b14a2838b4be6e273a20c57c53ec
7
+ data.tar.gz: 6735b0b2120ba48d9820851b090710bff0cb6286f43deb126a5fa48aae8e8115f0f164ff5f1c8454228bfa749f02e72250e255b71cc8ce95fe2e9f95a67ab59e
data/README.md CHANGED
@@ -8,7 +8,7 @@ Installation
8
8
 
9
9
  Add this to your Gemfile and run the `bundle` command.
10
10
 
11
- gem 'notches', '~> 0.6.0'
11
+ gem 'notches', '~> 0.7.1'
12
12
 
13
13
  And then install and run the necessary migrations.
14
14
 
@@ -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
@@ -7,10 +7,10 @@ class Notches::Hit < ActiveRecord::Base
7
7
 
8
8
  validates_associated :url, :session, :ip
9
9
 
10
- belongs_to :url, :foreign_key => :notches_url_id, :class_name => "URL"
10
+ belongs_to :url, :foreign_key => :notches_url_id, :class_name => "Notches::URL"
11
11
  belongs_to :user_agent, :foreign_key => :notches_user_agent_id
12
12
  belongs_to :session, :foreign_key => :notches_session_id
13
- belongs_to :ip, :foreign_key => :notches_ip_id, :class_name => "IP"
13
+ belongs_to :ip, :foreign_key => :notches_ip_id, :class_name => "Notches::IP"
14
14
  belongs_to :date, :foreign_key => :notches_date_id
15
15
  belongs_to :time, :foreign_key => :notches_time_id
16
16
 
@@ -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.1'
3
3
  end
data/notches.gemspec CHANGED
@@ -23,6 +23,6 @@ Gem::Specification.new do |s|
23
23
  s.add_runtime_dependency 'rails', '>= 4.1'
24
24
 
25
25
  s.add_development_dependency 'combustion', '~> 0.5.5'
26
- s.add_development_dependency 'rspec-rails', '~> 3.4.2'
26
+ s.add_development_dependency 'rspec-rails', '~> 3.9.1'
27
27
  s.add_development_dependency 'sqlite3', '~> 1.3.11'
28
28
  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,15 +1,15 @@
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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cillian O'Ruanaidh
8
8
  - Paul Campbell
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-08-04 00:00:00.000000000 Z
12
+ date: 2023-03-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -45,14 +45,14 @@ dependencies:
45
45
  requirements:
46
46
  - - "~>"
47
47
  - !ruby/object:Gem::Version
48
- version: 3.4.2
48
+ version: 3.9.1
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
53
  - - "~>"
54
54
  - !ruby/object:Gem::Version
55
- version: 3.4.2
55
+ version: 3.9.1
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: sqlite3
58
58
  requirement: !ruby/object:Gem::Requirement
@@ -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
@@ -116,7 +117,7 @@ files:
116
117
  homepage: http://github.com/hypertiny/notches
117
118
  licenses: []
118
119
  metadata: {}
119
- post_install_message:
120
+ post_install_message:
120
121
  rdoc_options: []
121
122
  require_paths:
122
123
  - lib
@@ -132,8 +133,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
133
  version: '0'
133
134
  requirements: []
134
135
  rubyforge_project: notches
135
- rubygems_version: 2.5.1
136
- signing_key:
136
+ rubygems_version: 2.7.6.2
137
+ signing_key:
137
138
  specification_version: 4
138
139
  summary: Simple Rails web traffic counter
139
140
  test_files: []