sentient_user_request_store 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 072578849d62f4c14e46bbba9298078673e22a56
4
+ data.tar.gz: d214388ec0381993cb9e2aec98d2cee61ca7217e
5
+ SHA512:
6
+ metadata.gz: 679bc68b2d1b05f2fdf4dd6a33644d35c84d9d562d659bc8e4f031065b0f5070ba1752805aa23da1c2a8f7705d406b669ffad3781c11f6eedc4f7a3d8103a7e0
7
+ data.tar.gz: bed93e79a4c018610a3f890f008434d016ba9a93c5c5690c8673ee113d0b654b90831dca83ea82cc1bb8d338fb7bf7a238a848a185f322f41159d2ace613ca44
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,23 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ .bundle
23
+ vendor
@@ -0,0 +1 @@
1
+ 2.3.0
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ sudo: false
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1.0
7
+ - 2.1.5
8
+ - 2.2.0
9
+ - 2.3.0
10
+ - jruby
11
+ before_install:
12
+ - gem update bundler
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at [dbock at javaguy dot org]. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,72 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ sentient_user_request_store (1.0.0)
5
+ railties (>= 3.1)
6
+ request_store (= 1.4.1)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionpack (4.0.13)
12
+ activesupport (= 4.0.13)
13
+ builder (~> 3.1.0)
14
+ erubis (~> 2.7.0)
15
+ rack (~> 1.5.2)
16
+ rack-test (~> 0.6.2)
17
+ activesupport (4.0.13)
18
+ i18n (~> 0.6, >= 0.6.9)
19
+ minitest (~> 4.2)
20
+ multi_json (~> 1.3)
21
+ thread_safe (~> 0.1)
22
+ tzinfo (~> 0.3.37)
23
+ ansi (1.5.0)
24
+ builder (3.1.4)
25
+ concurrent-ruby (1.0.5)
26
+ docile (1.1.5)
27
+ erubis (2.7.0)
28
+ i18n (0.9.5)
29
+ concurrent-ruby (~> 1.0)
30
+ json (1.8.3)
31
+ minitest (4.7.5)
32
+ minitest_should (0.3.1)
33
+ multi_json (1.13.1)
34
+ rack (1.5.5)
35
+ rack-test (0.6.3)
36
+ rack (>= 1.0)
37
+ railties (4.0.13)
38
+ actionpack (= 4.0.13)
39
+ activesupport (= 4.0.13)
40
+ rake (>= 0.8.7)
41
+ thor (>= 0.18.1, < 2.0)
42
+ rake (11.1.2)
43
+ rdoc (4.2.2)
44
+ json (~> 1.4)
45
+ request_store (1.4.1)
46
+ rack (>= 1.4)
47
+ simplecov (0.11.2)
48
+ docile (~> 1.1.0)
49
+ json (~> 1.8)
50
+ simplecov-html (~> 0.10.0)
51
+ simplecov-html (0.10.0)
52
+ thor (0.20.0)
53
+ thread_safe (0.3.6)
54
+ turn (0.9.7)
55
+ ansi
56
+ minitest (~> 4)
57
+ tzinfo (0.3.54)
58
+
59
+ PLATFORMS
60
+ ruby
61
+
62
+ DEPENDENCIES
63
+ minitest (= 4.7.5)
64
+ minitest_should
65
+ rake
66
+ rdoc
67
+ sentient_user_request_store!
68
+ simplecov
69
+ turn
70
+
71
+ BUNDLED WITH
72
+ 1.13.5
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 bokmann
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,46 @@
1
+ = sentient_user
2
+
3
+ How often have you needed to access the current user in your models? This happens often if you are attempting intelligent logging or if you want the models to control some aspect of their access, etc. This is easy hackery that you can find in many blog entries, but I wanted a nice package for it, for several reasons:
4
+
5
+ * There are several different ways this can be accomplished, but only the variation that uses Thread.local is safe for use in thread-safe rails.
6
+ * Even using Thread.local is considered slightly hacky (but better than monkeypatching ActiveRecord::Base)
7
+ * Should fashions change in this area in Rails' future, I want one place to change it, and to keep my methods working.
8
+ * This concept generally breaks in the console, where there is no controller to set the user. This gem adds a make_current method on the user instance, so you can choose to find a user and execute the current_user method. That could be really useful in rake tasks, for instance.
9
+
10
+ == Fork Notes
11
+
12
+ This fork of [sentient_user](https://github.com/bokmann/sentient_user) uses [request_store](https://github.com/steveklabnik/request_store) instead of `Thread.current`.
13
+
14
+ == Usage
15
+ * include SentientUser in your user model
16
+ * include SentientController in your application controller
17
+
18
+ == Assumptions and limitations
19
+ * Your user model is called 'User'.
20
+ * You have a current_user method available to your controllers
21
+ * You will write your own tests to cover this code. The gem has tests, but its up to you to make sure you use this ability wisely.
22
+
23
+ If those assumptions don't work for you, feel free to unpack and hack. It's simple enough code to change. I purposefully didn't want to complicate it with an initializer, several config options, etc. as all that would blur this delightfully simple piece of code.
24
+
25
+
26
+ == Recent Changes
27
+ This gem has been remarkably stable for many years and many iterations of rails. The recent deprecation of before_filter in favor of before_action is making me update it.
28
+
29
+ * 1.0.0 - use Request Store
30
+ * 0.4.0 - for use on Rails 5 and beyond.
31
+ * 0.3.3 - for versions prior to rails 5.
32
+
33
+
34
+ == Note on Patches/Pull Requests
35
+
36
+ * Fork the project.
37
+ * Make your feature addition or bug fix.
38
+ * Add tests for it. This is important so I don't break it
39
+ unintentionally in a future version.
40
+ * Commit, do not mess with rakefile, version, or history.
41
+ (if you want to have your own version, that is fine but please bump the version in a commit by itself which I can ignore when I pull)
42
+ * Send me a pull request. Bonus points for topic branches.
43
+
44
+ == Copyright
45
+
46
+ Copyright (c) 2009-2016 bokmann. See LICENSE for details.
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require "bundler/gem_tasks"
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |test|
7
+ test.libs << 'lib' << 'test'
8
+ test.pattern = 'test/**/test_*.rb'
9
+ test.verbose = true
10
+ end
11
+
12
+ task :default => :test
13
+
14
+
15
+ require File.expand_path('../lib/sentient_user/version.rb', __FILE__)
@@ -0,0 +1,49 @@
1
+ require 'request_store'
2
+
3
+ module SentientUser
4
+
5
+ def self.included(base)
6
+ base.class_eval {
7
+ def self.current
8
+ RequestStore.store[:user]
9
+ end
10
+
11
+ def self.current=(o)
12
+ raise(ArgumentError,
13
+ "Expected an object of class '#{self}', got #{o.inspect}") unless (o.is_a?(self) || o.nil?)
14
+ RequestStore.store[:user] = o
15
+ end
16
+
17
+ def make_current
18
+ RequestStore.store[:user] = self
19
+ end
20
+
21
+ def current?
22
+ !RequestStore.store[:user].nil? && self.id == RequestStore.store[:user].id
23
+ end
24
+
25
+ def self.do_as(user, &block)
26
+ old_user = self.current
27
+
28
+ begin
29
+ self.current = user
30
+ response = block.call unless block.nil?
31
+ ensure
32
+ self.current = old_user
33
+ end
34
+
35
+ response
36
+ end
37
+ }
38
+ end
39
+ end
40
+
41
+ module SentientController
42
+ def self.included(base)
43
+ base.class_eval {
44
+ before_action do |c|
45
+ User.current = c.send(:current_user)
46
+ end
47
+ }
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module SentientUser
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/sentient_user/version.rb', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["bokmann"]
6
+ gem.email = ["dbock@javaguy.org"]
7
+ gem.description = %q{lets the User model in most authentication frameworks know who is the current user}
8
+ gem.summary = %q{A trivial bit of common code}
9
+ gem.homepage = "http://github.com/house9/sentient_user_request_store"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "sentient_user_request_store"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = SentientUser::VERSION
17
+
18
+ gem.add_dependency "railties", ">= 3.1"
19
+ gem.add_dependency 'request_store', '1.4.1'
20
+
21
+ gem.add_development_dependency "rake"
22
+ gem.add_development_dependency "rdoc"
23
+ gem.add_development_dependency "minitest", "4.7.5"
24
+ gem.add_development_dependency 'minitest_should'
25
+ gem.add_development_dependency 'turn'
26
+
27
+ gem.add_development_dependency "simplecov"
28
+
29
+ end
@@ -0,0 +1,40 @@
1
+ require 'rubygems'
2
+ #require 'test/unit'
3
+ require "minitest/autorun"
4
+ require "minitest/should"
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ require 'sentient_user'
9
+
10
+ #class Test::Unit::TestCase
11
+ #end
12
+
13
+ class Person
14
+ include SentientUser
15
+ end
16
+
17
+ class User
18
+ include SentientUser
19
+ end
20
+
21
+ class AnonymousUser < User ; end
22
+
23
+ ExceptedWords = %w{ hackery hacky monkeypatching
24
+ ActiveRecord SentientUser SentientController
25
+ initializer config rakefile bokmann
26
+ sublicense MERCHANTABILITY NONINFRINGEMENT img src
27
+ }
28
+
29
+ def check_spelling_in_file(path_relative_to_gem_root)
30
+ path = "#{File.dirname(__FILE__)}/../#{path_relative_to_gem_root}"
31
+ begin
32
+ aspell_output = `cat #{path} | aspell list`
33
+ rescue => err
34
+ warn "You probably don't have aspell. On mac: brew install aspell --lang=en"
35
+ raise err
36
+ end
37
+ noticed_words = aspell_output.split($/)
38
+ misspellings = noticed_words - ExceptedWords
39
+ assert_equal [], misspellings
40
+ end
@@ -0,0 +1,57 @@
1
+ require 'helper'
2
+
3
+ class TestTruth < Minitest::Should::TestCase
4
+ should "allow making the 'person' class sentient" do
5
+ p = Person.new
6
+ p.make_current
7
+ assert_equal Person.current, p
8
+ end
9
+
10
+ should "allow making the 'user' class sentient" do
11
+ u = User.new
12
+ u.make_current
13
+ assert_equal User.current, u
14
+ end
15
+
16
+ should "not allow making Person.current a user" do
17
+ assert_raises ArgumentError do
18
+ Person.current = User.new
19
+ end
20
+ end
21
+
22
+ should "allow making person.current a person" do
23
+ Person.current = Person.new
24
+ end
25
+
26
+ should "allow subclasses of user to be assigned to user.current" do
27
+ User.current = AnonymousUser.new
28
+ end
29
+
30
+ should "allow execution of a block as a specified user" do
31
+ p, p2 = Person.new, Person.new
32
+ p.make_current
33
+ Person.do_as(p2) { assert_equal Person.current, p2 }
34
+ end
35
+
36
+ should "should reset the original user after executing a block as a specified user" do
37
+ p, p2 = Person.new, Person.new
38
+ p.make_current
39
+
40
+ Person.do_as(p2) { }
41
+ assert_equal Person.current, p
42
+
43
+ begin
44
+ Person.do_as(p2) { raise "error" }
45
+ rescue
46
+ assert_equal Person.current, p
47
+ end
48
+ end
49
+
50
+ should "have no spelling errors in its README" do
51
+ check_spelling_in_file "README.rdoc"
52
+ end
53
+
54
+ should "have no spelling errors in the license" do
55
+ check_spelling_in_file "LICENSE"
56
+ end
57
+ end
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sentient_user_request_store
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - bokmann
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-04-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: request_store
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.4.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.4.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rdoc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 4.7.5
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 4.7.5
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest_should
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: turn
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: lets the User model in most authentication frameworks know who is the
126
+ current user
127
+ email:
128
+ - dbock@javaguy.org
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".document"
134
+ - ".gitignore"
135
+ - ".rbenv-version"
136
+ - ".travis.yml"
137
+ - CODE_OF_CONDUCT.md
138
+ - Gemfile
139
+ - Gemfile.lock
140
+ - LICENSE
141
+ - README.rdoc
142
+ - Rakefile
143
+ - lib/sentient_user.rb
144
+ - lib/sentient_user/version.rb
145
+ - sentient_user_request_store.gemspec
146
+ - test/helper.rb
147
+ - test/test_sentient_user.rb
148
+ homepage: http://github.com/house9/sentient_user_request_store
149
+ licenses: []
150
+ metadata: {}
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ requirements: []
166
+ rubyforge_project:
167
+ rubygems_version: 2.5.1
168
+ signing_key:
169
+ specification_version: 4
170
+ summary: A trivial bit of common code
171
+ test_files:
172
+ - test/helper.rb
173
+ - test/test_sentient_user.rb