activesupport-current_attributes 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2b109012074bb1ed8a31034fbc47b79f844333fa
4
+ data.tar.gz: 45e6b01cb1e3fce6daff93a13b008f1eb5527a30
5
+ SHA512:
6
+ metadata.gz: 98b5474e715b5f0bf4fa3947774b4db41766fa0e4affec54391b9f8e91e5ff85e86e760e3355f86e91e4526f3d1c8f5819f4a1ea60f6e8b0982fdf8b81e7783c
7
+ data.tar.gz: 6d0549c0849fee4100ee5267cdf8474181c046636bddcb09c24e11371531ccc3152ea545da5b9f37df14a3564f37c78608dd8775de0d87655337b6a738862418
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Ahmed Hazem
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,114 @@
1
+ # Active Support Current Attributes
2
+
3
+ [![Build Status](https://travis-ci.com/coup-mobility/activesupport-current_attributes.svg?token=BjB77zxGgewb2s2spui7&branch=master)](https://travis-ci.com/coup-mobility/activesupport-current_attributes)
4
+
5
+ Provides a thread-isolated attributes singleton for Rails applications pre v5.2.0. The singleton is reset automatically before and after reach request. This allows you to keep all the per-request attributes easily available to the whole system.
6
+
7
+ _**Please note that**: This gem was extracted from this [commit](https://github.com/rails/rails/commit/24a864437e845febe91e3646ca008e8dc7f76b56) (lands in Rails v5.2.0). It is compatible with Rails >= 4.2._
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'activesupport-current_attributes'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install activesupport-current_attributes
24
+
25
+ ## Usage
26
+
27
+ The following full app-like example demonstrates how to use a `Current` class to facilitate easy access to the global, per-request attributes without passing them deeply around everywhere:
28
+
29
+ ```ruby
30
+ # app/models/current.rb
31
+ class Current < ActiveSupport::CurrentAttributes
32
+ attribute :account, :user
33
+ attribute :request_id, :user_agent, :ip_address
34
+
35
+ resets { Time.zone = nil }
36
+
37
+ def user=(user)
38
+ super
39
+ self.account = user.account
40
+ Time.zone = user.time_zone
41
+ end
42
+ end
43
+
44
+ # app/controllers/concerns/authentication.rb
45
+ module Authentication
46
+ extend ActiveSupport::Concern
47
+
48
+ included do
49
+ before_action :authenticate
50
+ end
51
+
52
+ private
53
+ def authenticate
54
+ if authenticated_user = User.find(cookies.signed[:user_id])
55
+ Current.user = authenticated_user
56
+ else
57
+ redirect_to new_session_url
58
+ end
59
+ end
60
+ end
61
+
62
+ # app/controllers/concerns/set_current_request_details.rb
63
+ module SetCurrentRequestDetails
64
+ extend ActiveSupport::Concern
65
+
66
+ included do
67
+ before_action do
68
+ Current.request_id = request.uuid
69
+ Current.user_agent = request.user_agent
70
+ Current.ip_address = request.ip
71
+ end
72
+ end
73
+ end
74
+
75
+ # app/controllers/application_controller.rb
76
+ class ApplicationController < ActionController::Base
77
+ include Authentication
78
+ include SetCurrentRequestDetails
79
+ end
80
+
81
+ # app/controllers/messages_controller.rb
82
+ class MessagesController < ApplicationController
83
+ def create
84
+ Current.account.messages.create(message_params)
85
+ end
86
+ end
87
+
88
+ # app/models/message.rb
89
+ class Message < ApplicationRecord
90
+ belongs_to :creator, default: -> { Current.user }
91
+ after_create { |message| Event.create(record: message) }
92
+ end
93
+
94
+ # app/models/event.rb
95
+ class Event < ApplicationRecord
96
+ before_create do
97
+ self.request_id = Current.request_id
98
+ self.user_agent = Current.user_agent
99
+ self.ip_address = Current.ip_address
100
+ end
101
+ end
102
+ ```
103
+
104
+ ## Contributing
105
+
106
+ 1. Fork it
107
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
108
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
109
+ 4. Push to the branch (`git push origin my-new-feature`)
110
+ 5. Create a new pull request
111
+
112
+ ## License
113
+
114
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,194 @@
1
+ module ActiveSupport
2
+ # Abstract super class that provides a thread-isolated attributes singleton, which resets automatically
3
+ # before and after reach request. This allows you to keep all the per-request attributes easily
4
+ # available to the whole system.
5
+ #
6
+ # The following full app-like example demonstrates how to use a Current class to
7
+ # facilitate easy access to the global, per-request attributes without passing them deeply
8
+ # around everywhere:
9
+ #
10
+ # # app/models/current.rb
11
+ # class Current < ActiveSupport::CurrentAttributes
12
+ # attribute :account, :user
13
+ # attribute :request_id, :user_agent, :ip_address
14
+ #
15
+ # resets { Time.zone = nil }
16
+ #
17
+ # def user=(user)
18
+ # super
19
+ # self.account = user.account
20
+ # Time.zone = user.time_zone
21
+ # end
22
+ # end
23
+ #
24
+ # # app/controllers/concerns/authentication.rb
25
+ # module Authentication
26
+ # extend ActiveSupport::Concern
27
+ #
28
+ # included do
29
+ # before_action :authenticate
30
+ # end
31
+ #
32
+ # private
33
+ # def authenticate
34
+ # if authenticated_user = User.find(cookies.signed[:user_id])
35
+ # Current.user = authenticated_user
36
+ # else
37
+ # redirect_to new_session_url
38
+ # end
39
+ # end
40
+ # end
41
+ #
42
+ # # app/controllers/concerns/set_current_request_details.rb
43
+ # module SetCurrentRequestDetails
44
+ # extend ActiveSupport::Concern
45
+ #
46
+ # included do
47
+ # before_action do
48
+ # Current.request_id = request.uuid
49
+ # Current.user_agent = request.user_agent
50
+ # Current.ip_address = request.ip
51
+ # end
52
+ # end
53
+ # end
54
+ #
55
+ # class ApplicationController < ActionController::Base
56
+ # include Authentication
57
+ # include SetCurrentRequestDetails
58
+ # end
59
+ #
60
+ # class MessagesController < ApplicationController
61
+ # def create
62
+ # Current.account.messages.create(message_params)
63
+ # end
64
+ # end
65
+ #
66
+ # class Message < ApplicationRecord
67
+ # belongs_to :creator, default: -> { Current.user }
68
+ # after_create { |message| Event.create(record: message) }
69
+ # end
70
+ #
71
+ # class Event < ApplicationRecord
72
+ # before_create do
73
+ # self.request_id = Current.request_id
74
+ # self.user_agent = Current.user_agent
75
+ # self.ip_address = Current.ip_address
76
+ # end
77
+ # end
78
+ #
79
+ # A word of caution: It's easy to overdo a global singleton like Current and tangle your model as a result.
80
+ # Current should only be used for a few, top-level globals, like account, user, and request details.
81
+ # The attributes stuck in Current should be used by more or less all actions on all requests. If you start
82
+ # sticking controller-specific attributes in there, you're going to create a mess.
83
+ class CurrentAttributes
84
+ include ActiveSupport::Callbacks
85
+ define_callbacks :reset
86
+
87
+ class << self
88
+ # Returns singleton instance for this class in this thread. If none exists, one is created.
89
+ def instance
90
+ Thread.current[:"current_attributes_for_#{name}"] ||= new.tap do |instance|
91
+ current_instances << instance
92
+ end
93
+ end
94
+
95
+ # Declares one or more attributes that will be given both class and instance accessor methods.
96
+ def attribute(*names)
97
+ generated_attribute_methods.module_eval do
98
+ names.each do |name|
99
+ define_method(name) do
100
+ attributes[name.to_sym]
101
+ end
102
+
103
+ define_method("#{name}=") do |attribute|
104
+ attributes[name.to_sym] = attribute
105
+ end
106
+ end
107
+ end
108
+
109
+ names.each do |name|
110
+ define_singleton_method(name) do
111
+ instance.public_send(name)
112
+ end
113
+
114
+ define_singleton_method("#{name}=") do |attribute|
115
+ instance.public_send("#{name}=", attribute)
116
+ end
117
+ end
118
+ end
119
+
120
+ # Calls this block after #reset is called on the instance. Used for resetting external collaborators, like Time.zone.
121
+ def resets(&block)
122
+ set_callback :reset, :after, &block
123
+ end
124
+
125
+ delegate :set, :reset, to: :instance
126
+
127
+ def reset_all # :nodoc:
128
+ current_instances.each(&:reset)
129
+ end
130
+
131
+ private
132
+ def generated_attribute_methods
133
+ @generated_attribute_methods ||= Module.new.tap { |mod| include mod }
134
+ end
135
+
136
+ def current_instances
137
+ Thread.current[:current_attributes_instances] ||= []
138
+ end
139
+
140
+ def method_missing(name, *args, &block)
141
+ # Caches the method definition as a singleton method of the receiver.
142
+ #
143
+ # By letting #delegate handle it, we avoid an enclosure that'll capture args.
144
+ singleton_class.delegate name, to: :instance
145
+
146
+ send(name, *args, &block)
147
+ end
148
+ end
149
+
150
+ attr_accessor :attributes
151
+
152
+ def initialize
153
+ @attributes = {}
154
+ end
155
+
156
+ # Expose one or more attributes within a block. Old values are returned after the block concludes.
157
+ # Example demonstrating the common use of needing to set Current attributes outside the request-cycle:
158
+ #
159
+ # class Chat::PublicationJob < ApplicationJob
160
+ # def perform(attributes, room_number, creator)
161
+ # Current.set(person: creator) do
162
+ # Chat::Publisher.publish(attributes: attributes, room_number: room_number)
163
+ # end
164
+ # end
165
+ # end
166
+ def set(set_attributes)
167
+ old_attributes = compute_attributes(set_attributes.keys)
168
+ assign_attributes(set_attributes)
169
+ yield
170
+ ensure
171
+ assign_attributes(old_attributes)
172
+ end
173
+
174
+ # Reset all attributes. Should be called before and after actions, when used as a per-request singleton.
175
+ def reset
176
+ run_callbacks :reset do
177
+ self.attributes = {}
178
+ end
179
+ end
180
+
181
+ private
182
+ def assign_attributes(new_attributes)
183
+ new_attributes.each { |key, value| public_send("#{key}=", value) }
184
+ end
185
+
186
+ def compute_attributes(keys)
187
+ if [].respond_to?(:to_h)
188
+ keys.collect { |key| [ key, public_send(key) ] }.to_h
189
+ else
190
+ keys.each_with_object({}) { |key, attributes| attributes[key] = public_send(key) }
191
+ end
192
+ end
193
+ end
194
+ end
@@ -0,0 +1,10 @@
1
+ require 'active_support'
2
+
3
+ module ActiveSupport
4
+ class Railtie < Rails::Railtie # :nodoc:
5
+ initializer "active_support.reset_all_current_attributes_instances", before: "active_support.deprecation_behavior" do |app|
6
+ app.executor.to_run { ActiveSupport::CurrentAttributes.reset_all }
7
+ app.executor.to_complete { ActiveSupport::CurrentAttributes.reset_all }
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module CurrentAttributes
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,3 @@
1
+ require "active_support/current_attributes/current_attributes"
2
+ require "active_support/current_attributes/railtie" if defined? Rails::Railtie
3
+ require "active_support/current_attributes/version"
@@ -0,0 +1 @@
1
+ require "active_support/current_attributes"
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activesupport-current_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Coup Mobility GmbH
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack-test
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.6'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5.0'
97
+ description: " Provides a thread-isolated attributes singleton for Rails applications
98
+ pre v5.2.0. "
99
+ email:
100
+ - ahmed.hemdan@joincoup.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - LICENSE.txt
106
+ - README.md
107
+ - lib/active_support/current_attributes.rb
108
+ - lib/active_support/current_attributes/current_attributes.rb
109
+ - lib/active_support/current_attributes/railtie.rb
110
+ - lib/active_support/current_attributes/version.rb
111
+ - lib/activesupport/current_attributes.rb
112
+ homepage: https://github.com/coup-mobility/activesupport-current_attributes
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.6.12
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: Provides a thread-isolated attributes singleton for Rails applications pre
136
+ v5.2.0.
137
+ test_files: []