mongoid-simple-userstamps 0.2.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 7123312aed4696e240e1e23468c2454b841ba67d
4
- data.tar.gz: 819baa4411adafdf3357a4b56a46daaadc3a3e36
2
+ SHA256:
3
+ metadata.gz: 81b8a57413bd388e7166eafb49105d1ea7c7e37b7ce952e99a06532af6d41522
4
+ data.tar.gz: 4e52b4597c4be8f096cb67b6f31b2c14708e5edf57ef035657d1bc20b8a7a0da
5
5
  SHA512:
6
- metadata.gz: e3a8461955bc75e1e2d26ba54d5a9962ded5913a718b351157af9a33fa95f443ea984e8b3a0842da4daf769e0adbb5e74ebd338edca5ad65e799558d58bef0a4
7
- data.tar.gz: 6dd9aff620666f58b06249aa042a279cc4877f8ee85904640e117ccf422b6a33216d5a41c02aa4992e5abfe46dcf4a9c3cd28a21558f3e4d0292db2818dacc98
6
+ metadata.gz: 939f8d2fce4291e8f77823df64d19f81f238519d6b7886eabb066747d16c08f55803f9832d026de4e5744eade39d7225a86e2422d5e3f0e1f351c831da14fe3c
7
+ data.tar.gz: 5401425c074473d84f67df1dcd5a38a5a529a93c537e7f65030a20706cd2eb4905764ff8789e18c9d52ec8f7445471b976bb46406157d6bdfa9fdaf80d19aad5
data/.travis.yml ADDED
@@ -0,0 +1,21 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.1
6
+
7
+ gemfile:
8
+ - Gemfile
9
+ - gemfiles/mongoid-5.0.gemfile
10
+ - gemfiles/mongoid-6.0.gemfile
11
+ - gemfiles/mongoid-7.0.gemfile
12
+
13
+ services:
14
+ - mongodb
15
+
16
+ addons:
17
+ apt:
18
+ sources:
19
+ - mongodb-3.4-precise
20
+ packages:
21
+ - mongodb-org-server
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in mongoid-normalize-strings.gemspec
4
4
  gemspec
5
+
6
+ gem 'rake'
data/README.md CHANGED
@@ -4,7 +4,7 @@ Adds stamps relations to mongoid models.
4
4
 
5
5
  ## Version Support
6
6
 
7
- Mongoid 5.x, 6.x
7
+ `5.x, 6.x and 7.x`
8
8
 
9
9
  ## Installation
10
10
 
@@ -22,14 +22,12 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- * Include `Mongoid::Userstamps::User` in your 'User' model.
26
- * Include `Mongoid::Userstamps::Model` in your models you want user stamps.
27
- * Define current user.
25
+ - Include `Mongoid::Userstamps::Model` in your models you want user stamps.
26
+ - Define current user throught `Mongoid::Userstamps::User.current`.
28
27
 
29
28
  ```ruby
30
29
  class User
31
30
  include Mongoid::Document
32
- include Mongoid::Userstamps::User
33
31
  end
34
32
 
35
33
  class Post
@@ -40,7 +38,7 @@ Or install it yourself as:
40
38
  end
41
39
 
42
40
  user = User.create
43
- Mongoid::Userstamps::Config.set_current(user)
41
+ Mongoid::Userstamps::User.current = user
44
42
 
45
43
  pòst = Post.create
46
44
  post.created_by # <User _id: 57305c268675c32e1c70a17e >
@@ -53,25 +51,36 @@ Or install it yourself as:
53
51
 
54
52
  ### Rails
55
53
 
56
- * Define current user in your `application_controller.rb`
54
+ - Define current user in your `application_controller.rb`
57
55
 
58
56
  ```ruby
59
57
  class ApplicationController < ActionController::Base
58
+ before_action :authenticate!
60
59
  before_action :define_userstamps_current
61
60
 
62
61
  protected
63
62
 
64
63
  def define_userstamps_current
65
- Mongoid::Userstamps::Config.set_current(current_user)
64
+ Mongoid::Userstamps::User.current = current_user
66
65
  end
67
66
  end
68
67
  ```
69
68
 
69
+ ### Warden
70
+
71
+ * Define current user in [warden callback](https://github.com/hassox/warden/wiki/Callbacks)
72
+
73
+ ```ruby
74
+ Warden::Manager.after_set_user do |user, auth, opts|
75
+ Mongoid::Userstamps::Config.set_current(record)
76
+ end
77
+ ```
78
+
70
79
  ## Contributing
71
80
 
72
- 1. Fork it (
73
- http://github.com/<my-github-username>/mongoid-simple-userstamps/fork )
74
- 2. Create your feature branch (`git checkout -b my-new-feature`)
75
- 3. Commit your changes (`git commit -am 'Add some feature'`)
76
- 4. Push to the branch (`git push origin my-new-feature`)
77
- 5. Create new Pull Request
81
+ 1. Fork it (
82
+ <http://github.com/><my-github-username>/mongoid-simple-userstamps/fork )
83
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
84
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
85
+ 4. Push to the branch (`git push origin my-new-feature`)
86
+ 5. Create new Pull Request
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid-normalize-strings.gemspec
4
+ gemspec path: '../'
5
+
6
+ gem 'rake'
7
+ gem 'mongoid', '~> 5.0'
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid-normalize-strings.gemspec
4
+ gemspec path: '../'
5
+
6
+ gem 'rake'
7
+ gem 'mongoid', '~> 6.0'
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid-normalize-strings.gemspec
4
+ gemspec path: '../'
5
+
6
+ gem 'rake'
7
+ gem 'mongoid', '~> 7.0'
@@ -1,4 +1,3 @@
1
- require "mongoid-simple-userstamps/version"
2
- require "mongoid-simple-userstamps/config"
3
- require "mongoid-simple-userstamps/user"
4
- require "mongoid-simple-userstamps/model"
1
+ require 'mongoid-simple-userstamps/version'
2
+ require 'mongoid-simple-userstamps/user'
3
+ require 'mongoid-simple-userstamps/model'
@@ -5,18 +5,21 @@ module Mongoid
5
5
  module Userstamps
6
6
  module Model
7
7
  def self.included(base)
8
- base.send(:belongs_to, :created_by, polymorphic: true, optional: true)
9
- base.send(:belongs_to, :updated_by, polymorphic: true, optional: true)
8
+ base.class_eval do
9
+ if Mongoid::VERSION.to_f < 6
10
+ belongs_to :created_by, polymorphic: true
11
+ belongs_to :updated_by, polymorphic: true
12
+ else
13
+ belongs_to :created_by, polymorphic: true, optional: true
14
+ belongs_to :updated_by, polymorphic: true, optional: true
15
+ end
10
16
 
11
- base.send(:before_create) do
12
- if Mongoid::Userstamps::Config.current && !self.created_by
13
- self.created_by = Mongoid::Userstamps::Config.current
17
+ before_create do
18
+ self.created_by = Mongoid::Userstamps::User.current unless created_by
14
19
  end
15
- end
16
20
 
17
- base.send(:before_update) do
18
- if Mongoid::Userstamps::Config.current
19
- self.updated_by = Mongoid::Userstamps::Config.current
21
+ before_update do
22
+ self.updated_by = Mongoid::Userstamps::User.current
20
23
  end
21
24
  end
22
25
  end
@@ -4,8 +4,12 @@
4
4
  module Mongoid
5
5
  module Userstamps
6
6
  module User
7
- def self.included(base)
8
- Mongoid::Userstamps::Config.set_model(self)
7
+ def self.current
8
+ Thread.current[:current_user]
9
+ end
10
+
11
+ def self.current=(current)
12
+ Thread.current[:current_user] = current
9
13
  end
10
14
  end
11
15
  end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Userstamps
3
- VERSION = "0.2.0"
3
+ VERSION = '1.0.0'
4
4
  end
5
5
  end
@@ -1,24 +1,25 @@
1
1
  # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
2
+
3
+ lib = File.expand_path('lib', __dir__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'mongoid-simple-userstamps/version'
5
6
 
6
7
  Gem::Specification.new do |spec|
7
- spec.name = "mongoid-simple-userstamps"
8
+ spec.name = 'mongoid-simple-userstamps'
8
9
  spec.version = Mongoid::Userstamps::VERSION
9
- spec.authors = ["Rafael Jurado"]
10
- spec.email = ["rjurado@nosolosoftware.es"]
11
- spec.summary = %q{Simple way to add user stamps to your models.}
12
- spec.description = %q{Simple way to add user stamps to your models.}
13
- spec.homepage = ""
14
- spec.license = "MIT"
10
+ spec.authors = ['Rafael Jurado']
11
+ spec.email = ['rjurado@nosolosoftware.es']
12
+ spec.summary = 'Simple way to add user stamps to your models.'
13
+ spec.description = 'Simple way to add user stamps to your models.'
14
+ spec.homepage = ''
15
+ spec.license = 'MIT'
15
16
 
16
17
  spec.files = `git ls-files -z`.split("\x0")
17
18
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
20
+ spec.require_paths = ['lib']
20
21
 
21
- spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency 'bundler', '~> 1.16'
22
23
  spec.add_development_dependency 'rspec'
23
- spec.add_dependency 'mongoid', ">= 5.0.0"
24
+ spec.add_dependency 'mongoid', '>= 5.0.0'
24
25
  end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'mongoid'
2
2
  require 'mongoid-simple-userstamps'
3
3
 
4
- Mongoid.load!("spec/support/mongoid.yml", :test)
4
+ Mongoid.load!('spec/support/mongoid.yml', :test)
5
5
 
6
6
  Mongoid.logger.level = Logger::ERROR
7
7
  Mongo::Logger.logger.level = Logger::ERROR
@@ -3,7 +3,6 @@ require 'spec_helper'
3
3
  describe 'Mongoid::Userstamp' do
4
4
  class User
5
5
  include Mongoid::Document
6
- include Mongoid::Userstamps::User
7
6
  end
8
7
 
9
8
  class Post
@@ -16,7 +15,7 @@ describe 'Mongoid::Userstamp' do
16
15
  context 'when current is defined' do
17
16
  before :all do
18
17
  @user = User.create
19
- Mongoid::Userstamps::Config.set_current(@user)
18
+ Mongoid::Userstamps::User.current = @user
20
19
  end
21
20
 
22
21
  context 'when include Mongoid::Userstamps::Model' do
@@ -48,24 +47,52 @@ describe 'Mongoid::Userstamp' do
48
47
  expect(post.updated_by).to eq(@user)
49
48
  end
50
49
  end
50
+
51
+ context 'when check thread safe' do
52
+ it 'works' do
53
+ Post.destroy_all
54
+ user1 = User.create
55
+ user2 = User.create
56
+
57
+ Thread.start do
58
+ Mongoid::Userstamps::User.current = user1
59
+ sleep 0.2
60
+ Post.create
61
+ end
62
+
63
+ Thread.start do
64
+ sleep 0.1
65
+ Mongoid::Userstamps::User.current = user2
66
+ end
67
+
68
+ sleep 0.3
69
+ expect(Post.first.created_by).to eq(user1)
70
+ end
71
+ end
51
72
  end
52
73
 
53
74
  context 'when current is not defined' do
54
75
  before :all do
55
- Mongoid::Userstamps::Config.set_current(nil)
76
+ Mongoid::Userstamps::User.current = nil
56
77
  end
57
78
 
58
79
  it 'should not set created_by' do
59
- user = User.create
60
- post = Post.create!(created_by: user)
61
- expect(post.created_by).to eq(user)
80
+ post = Post.create
81
+ expect(post.created_by).to be_nil
62
82
  end
63
83
 
64
84
  it 'should not set updated_by' do
65
- user = User.create
66
- post = Post.create!
67
- post.update_attributes(title: 'title', updated_by: user)
68
- expect(post.updated_by).to eq(user)
85
+ post = Post.create
86
+ post.update_attributes(title: 'title')
87
+ expect(post.updated_by).to be_nil
88
+ end
89
+
90
+ context 'when set manually created_by' do
91
+ it 'should respect it' do
92
+ user = User.create
93
+ post = Post.create(created_by: user)
94
+ expect(post.created_by).to eq(user)
95
+ end
69
96
  end
70
97
  end
71
98
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-simple-userstamps
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rafael Jurado
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-07 00:00:00.000000000 Z
11
+ date: 2018-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.5'
19
+ version: '1.16'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.5'
26
+ version: '1.16'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -60,12 +60,15 @@ extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
62
  - ".gitignore"
63
+ - ".travis.yml"
63
64
  - Gemfile
64
65
  - LICENSE.txt
65
66
  - README.md
66
67
  - Rakefile
68
+ - gemfiles/mongoid-5.0.gemfile
69
+ - gemfiles/mongoid-6.0.gemfile
70
+ - gemfiles/mongoid-7.0.gemfile
67
71
  - lib/mongoid-simple-userstamps.rb
68
- - lib/mongoid-simple-userstamps/config.rb
69
72
  - lib/mongoid-simple-userstamps/model.rb
70
73
  - lib/mongoid-simple-userstamps/user.rb
71
74
  - lib/mongoid-simple-userstamps/version.rb
@@ -93,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
96
  version: '0'
94
97
  requirements: []
95
98
  rubyforge_project:
96
- rubygems_version: 2.4.5.1
99
+ rubygems_version: 2.7.3
97
100
  signing_key:
98
101
  specification_version: 4
99
102
  summary: Simple way to add user stamps to your models.
@@ -101,4 +104,3 @@ test_files:
101
104
  - spec/spec_helper.rb
102
105
  - spec/support/mongoid.yml
103
106
  - spec/userstamps_spec.rb
104
- has_rdoc:
@@ -1,27 +0,0 @@
1
- ##
2
- # This module store Userstamps config
3
- #
4
- module Mongoid
5
- module Userstamps
6
- module Config
7
- @@model = nil
8
- @@current = nil
9
-
10
- def self.current
11
- @@current
12
- end
13
-
14
- def self.model
15
- @@model
16
- end
17
-
18
- def self.set_current(current)
19
- @@current = current
20
- end
21
-
22
- def self.set_model(user_class)
23
- @@user_class = user_class
24
- end
25
- end
26
- end
27
- end