Skip to content

Commit 0c3ce5c

Browse files
committed
Ask the user about their current user when generating a config file
1 parent 5560784 commit 0c3ce5c

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

lib/rails/generators/intercom/config/config_generator.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,34 @@ def self.source_root
1010
argument :api_secret, :desc => "Your Intercom api-secret, used for secure mode", :optional => true
1111
argument :api_key, :desc => "An Intercom API key, for various rake tasks", :optional => true
1212

13+
FALSEY_RESPONSES = ['n', 'no']
1314
def create_config_file
1415
@app_id = app_id
1516
@api_secret = api_secret
1617
@api_key = api_key
1718

19+
introduction = <<-desc
20+
Intercom will automatically insert its javascript before the closing '</body>'
21+
tag on every page where it can find a logged-in user. Intercom by default
22+
looks for logged-in users, in the controller, via 'current_user' and '@user'.
23+
24+
Is the logged-in user accessible via either 'current_user' or '@user'? [Yn]
25+
desc
26+
27+
print "#{introduction.strip} "
28+
default_ok = $stdin.gets.strip.downcase
29+
30+
if FALSEY_RESPONSES.include?(default_ok)
31+
custom_current_user_question = <<-desc
32+
33+
How do you access the logged-in user in your controllers? This can be
34+
any Ruby code, e.g. 'current_customer', '@admin', etc.:
35+
desc
36+
37+
print "#{custom_current_user_question.rstrip} "
38+
@current_user = $stdin.gets.strip
39+
end
40+
1841
template("intercom.rb.erb", "config/initializers/intercom.rb")
1942
end
2043

lib/rails/generators/intercom/config/intercom.rb.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ IntercomRails.config do |config|
2727
# The method/variable that contains the logged in user in your controllers.
2828
# If it is `current_user` or `@user`, then you can ignore this
2929
#
30+
<%- if @current_user -%>
31+
config.user.current = Proc.new { <%= @current_user %> }
32+
<%- else -%>
3033
# config.user.current = Proc.new { current_user }
34+
<%- end -%>
3135

3236
# == User model class
3337
# The class which defines your user model

0 commit comments

Comments
 (0)