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

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.5.1'.freeze

Class Method Summary collapse

Class Method Details

.check_deprecations(type) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/userlist/rails.rb', line 92

def self.check_deprecations(type)
  deprecated_methods = (type.instance_methods + type.private_instance_methods) & DEPRECATED_MODEL_METHODS

  if deprecated_methods.any?
    raise "      Deprecation warning for userlist-rails\n\n      Customizing the way userlist-rails works has changed.\n\n      Using the \#{deprecated_methods.to_sentence} method(s) on your \#{type.name} model is not supported anymore.\n\n      For details on how to customize the gem's behavior, please see https://github.com/userlist/userlist-rails or reach out to [email protected]\n    MESSAGE\n  end\n\n  deprecated_methods = type.private_instance_methods.grep(/userlist_/)\n\n  if deprecated_methods.any?\n    raise <<~MESSAGE\n      Deprecation warning for userlist-rails\n\n      Customizing the way userlist-rails works has changed.\n\n      Using private methods (like \#{deprecated_methods.to_sentence}) on your \#{type.name} model is not supported anymore. Please use public methods instead.\n\n      For details on how to customize the gem's behavior, please see https://github.com/userlist/userlist-rails or reach out to [email protected]\n    MESSAGE\n  end\n\n  true\nend\n"

.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
# File 'lib/userlist/rails.rb', line 36

def self.detect_model(*names)
  names.each do |name|
    begin
      return name.constantize
    rescue NameError
      false
    end
  end

  nil
end

.detect_relationship(from, to) ⇒ Object



48
49
50
51
52
# File 'lib/userlist/rails.rb', line 48

def self.detect_relationship(from, to)
  return unless reflection = find_reflection(from, to)

  reflection.through_reflection.klass if reflection.through_reflection?
end

.find_reflection(from, to) ⇒ Object



54
55
56
57
58
# File 'lib/userlist/rails.rb', line 54

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/userlist/rails.rb', line 70

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} #{method.to_s.singularize}: #{e.message}"
    end
  end

  model.public_send(callback_method, callback, on: type)
end

.setup_callbacks(model, scope) ⇒ Object



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

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



85
86
87
88
89
90
# File 'lib/userlist/rails.rb', line 85

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