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
VERSION =
'0.6.2'.freeze

Class Method Summary collapse

Class Method Details

.check_deprecations(type) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/userlist/rails.rb', line 100

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_companyObject



32
33
34
# File 'lib/userlist/rails.rb', line 32

def self.current_company
  Thread.current[:userlist_current_company]
end

.current_userObject



21
22
23
# File 'lib/userlist/rails.rb', line 21

def self.current_user
  Thread.current[:userlist_current_user]
end

.detect_model(*names) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/userlist/rails.rb', line 36

def self.detect_model(*names)
  names.each do |name|
    begin
      model = name.constantize

      return model if model.is_a?(Class)
    rescue NameError
      false
    end
  end

  nil
end

.detect_relationship(from, to) ⇒ Object



50
51
52
53
54
# File 'lib/userlist/rails.rb', line 50

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



62
63
64
65
66
# File 'lib/userlist/rails.rb', line 62

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



56
57
58
59
60
# File 'lib/userlist/rails.rb', line 56

def self.find_reflection(from, to)
  return unless from && to

  from.reflect_on_all_associations.find { |r| r.class_name == to.to_s }
end

.setup_callback(type, model, scope, method) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/userlist/rails.rb', line 78

def self.setup_callback(type, model, scope, method)
  return unless callback_method = [:after_commit, :"after_#{type}"].find { |m| model.respond_to?(m) }

  callback = lambda do
    begin
      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



68
69
70
71
72
73
74
75
76
# File 'lib/userlist/rails.rb', line 68

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_extensionsObject



93
94
95
96
97
98
# File 'lib/userlist/rails.rb', line 93

def self.setup_extensions
  Userlist::Push::User.include(Userlist::Rails::Extensions::User)
  Userlist::Push::Company.include(Userlist::Rails::Extensions::Company)
  Userlist::Push::Relationship.include(Userlist::Rails::Extensions::Relationship)
  Userlist::Push::Event.include(Userlist::Rails::Extensions::Event)
end

.with_current_company(company) ⇒ Object



25
26
27
28
29
30
# File 'lib/userlist/rails.rb', line 25

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



14
15
16
17
18
19
# File 'lib/userlist/rails.rb', line 14

def self.with_current_user(user)
  Thread.current[:userlist_current_user] = user
  yield
ensure
  Thread.current[:userlist_current_user] = nil
end