Module: Userlist::Rails
- Defined in:
- lib/userlist/rails.rb,
lib/userlist/rails/config.rb,
lib/userlist/rails/logger.rb,
lib/userlist/rails/helpers.rb,
lib/userlist/rails/railtie.rb,
lib/userlist/rails/version.rb,
lib/userlist/rails/importer.rb,
lib/userlist/rails/transform.rb,
lib/userlist/rails/extensions/user.rb,
lib/userlist/rails/transforms/user.rb,
lib/userlist/rails/extensions/event.rb,
lib/userlist/rails/extensions/company.rb,
lib/userlist/rails/transforms/company.rb,
lib/userlist/rails/extensions/relationship.rb,
lib/userlist/rails/transforms/relationship.rb,
lib/userlist/rails/transforms/has_relationships.rb
Defined Under Namespace
Modules: Config, Extensions, Helpers, Transforms
Classes: Importer, Logger, Railtie, Transform
Constant Summary
collapse
- DEPRECATED_MODEL_METHODS =
[
:userlist_push,
:userlist_delete,
:userlist_payload,
:userlist_company,
:userlist_user
].freeze
- REFLECTION_PRIORITY =
[
ActiveRecord::Reflection::ThroughReflection,
ActiveRecord::Reflection::HasManyReflection,
ActiveRecord::Reflection::HasOneReflection
].freeze
- VERSION =
'1.0.1'.freeze
Class Method Summary
collapse
Class Method Details
.check_deprecations(type) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/userlist/rails.rb', line 107
def self.check_deprecations(type)
deprecated_methods = (type.instance_methods + type.private_instance_methods) & DEPRECATED_MODEL_METHODS
if deprecated_methods.any?
raise <<~MESSAGE
Deprecation warning for userlist-rails
Customizing the way userlist-rails works has changed.
Using the #{deprecated_methods.to_sentence} method(s) on your #{type.name} model is not supported anymore.
For details on how to customize the gem's behavior, please see https://github.com/userlist/userlist-rails or reach out to [email protected]
MESSAGE
end
deprecated_methods = type.private_instance_methods.grep(/userlist_/)
if deprecated_methods.any?
raise <<~MESSAGE
Deprecation warning for userlist-rails
Customizing the way userlist-rails works has changed.
Using private methods (like #{deprecated_methods.to_sentence}) on your #{type.name} model is not supported anymore. Please use public methods instead.
For details on how to customize the gem's behavior, please see https://github.com/userlist/userlist-rails or reach out to [email protected]
MESSAGE
end
true
end
|
.current_company ⇒ Object
38
39
40
|
# File 'lib/userlist/rails.rb', line 38
def self.current_company
Thread.current[:userlist_current_company]
end
|
.current_user ⇒ Object
27
28
29
|
# File 'lib/userlist/rails.rb', line 27
def self.current_user
Thread.current[:userlist_current_user]
end
|
.detect_model(*names) ⇒ Object
42
43
44
45
46
47
48
49
50
|
# File 'lib/userlist/rails.rb', line 42
def self.detect_model(*names)
names.each do |name|
model = Object.const_get(name) if Object.const_defined?(name)
return model if model.is_a?(Class)
end
nil
end
|
.detect_relationship(from, to) ⇒ Object
52
53
54
55
56
|
# File 'lib/userlist/rails.rb', line 52
def self.detect_relationship(from, to)
return unless reflection = find_reflection(from, to)
reflection.through_reflection.klass if reflection.through_reflection?
end
|
.find_association_between(from, to) ⇒ Object
67
68
69
70
71
|
# File 'lib/userlist/rails.rb', line 67
def self.find_association_between(from, to)
return unless association = Userlist::Rails.find_reflection(from, to)
association.through_reflection || association
end
|
.find_reflection(from, to) ⇒ Object
58
59
60
61
62
63
64
65
|
# File 'lib/userlist/rails.rb', line 58
def self.find_reflection(from, to)
return unless from && to
from
.reflect_on_all_associations
.sort_by { |r| REFLECTION_PRIORITY.index(r.class) || REFLECTION_PRIORITY.length }
.find { |r| r.class_name == to.to_s }
end
|
.setup_callback(type, model, scope, default_method) ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/userlist/rails.rb', line 83
def self.setup_callback(type, model, scope, default_method)
return unless callback_method = [:after_commit, :"after_#{type}"].find { |m| model.respond_to?(m) }
callback = lambda do
begin
method = try(:"userlist_#{type}_behavior") || default_method
relation = Userlist::Push.public_send(scope)
relation.public_send(method, self)
rescue Userlist::Error => e
Userlist.logger.error "Failed to #{method} #{scope.to_s.singularize}: #{e.message}"
end
end
model.public_send(callback_method, callback, on: type)
end
|
.setup_callbacks(model, scope) ⇒ Object
73
74
75
76
77
78
79
80
81
|
# File 'lib/userlist/rails.rb', line 73
def self.setup_callbacks(model, scope)
return if model.instance_variable_get(:@userlist_callbacks_registered)
setup_callback(:create, model, scope, :push)
setup_callback(:update, model, scope, :push)
setup_callback(:destroy, model, scope, :delete)
model.instance_variable_set(:@userlist_callbacks_registered, true)
end
|
.setup_extensions ⇒ Object
.with_current_company(company) ⇒ Object
31
32
33
34
35
36
|
# File 'lib/userlist/rails.rb', line 31
def self.with_current_company(company)
Thread.current[:userlist_current_company] = company
yield
ensure
Thread.current[:userlist_current_company] = nil
end
|
.with_current_user(user) ⇒ Object
20
21
22
23
24
25
|
# File 'lib/userlist/rails.rb', line 20
def self.with_current_user(user)
Thread.current[:userlist_current_user] = user
yield
ensure
Thread.current[:userlist_current_user] = nil
end
|