Class: IntercomRails::ScriptTag
- Inherits:
-
Object
- Object
- IntercomRails::ScriptTag
- Defined in:
- lib/intercom-rails/script_tag.rb
Instance Attribute Summary collapse
-
#company_details ⇒ Object
readonly
Returns the value of attribute company_details.
-
#controller ⇒ Object
Returns the value of attribute controller.
-
#secret ⇒ Object
Returns the value of attribute secret.
-
#show_everywhere ⇒ Object
readonly
Returns the value of attribute show_everywhere.
-
#user_details ⇒ Object
readonly
Returns the value of attribute user_details.
-
#widget_options ⇒ Object
Returns the value of attribute widget_options.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ ScriptTag
constructor
A new instance of ScriptTag.
- #intercom_settings ⇒ Object
- #output ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ ScriptTag
Returns a new instance of ScriptTag.
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/intercom-rails/script_tag.rb', line 16 def initialize( = {}) self.secret = [:secret] || Config.api_secret self. = .merge([:widget] || {}) self.controller = [:controller] @show_everywhere = [:show_everywhere] self.user_details = [:find_current_user_details] ? find_current_user_details : [:user_details] self.company_details = if [:find_current_company_details] find_current_company_details elsif [:user_details] [:user_details].delete(:company) if [:user_details] end end |
Instance Attribute Details
#company_details ⇒ Object
Returns the value of attribute company_details.
13 14 15 |
# File 'lib/intercom-rails/script_tag.rb', line 13 def company_details @company_details end |
#controller ⇒ Object
Returns the value of attribute controller.
14 15 16 |
# File 'lib/intercom-rails/script_tag.rb', line 14 def controller @controller end |
#secret ⇒ Object
Returns the value of attribute secret.
14 15 16 |
# File 'lib/intercom-rails/script_tag.rb', line 14 def secret @secret end |
#show_everywhere ⇒ Object (readonly)
Returns the value of attribute show_everywhere.
13 14 15 |
# File 'lib/intercom-rails/script_tag.rb', line 13 def show_everywhere @show_everywhere end |
#user_details ⇒ Object
Returns the value of attribute user_details.
13 14 15 |
# File 'lib/intercom-rails/script_tag.rb', line 13 def user_details @user_details end |
#widget_options ⇒ Object
Returns the value of attribute widget_options.
14 15 16 |
# File 'lib/intercom-rails/script_tag.rb', line 14 def @widget_options end |
Class Method Details
.generate(*args) ⇒ Object
9 10 11 |
# File 'lib/intercom-rails/script_tag.rb', line 9 def self.generate(*args) new(*args).output end |
Instance Method Details
#intercom_settings ⇒ Object
37 38 39 40 41 42 |
# File 'lib/intercom-rails/script_tag.rb', line 37 def intercom_settings hsh = user_details hsh[:widget] = if .present? hsh[:company] = company_details if company_details.present? hsh end |
#output ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/intercom-rails/script_tag.rb', line 44 def output intercom_settings_json = ActiveSupport::JSON.encode(intercom_settings).gsub('<', '\u003C') str = <<-INTERCOM_SCRIPT <script id="IntercomSettingsScriptTag"> window.intercomSettings = #{intercom_settings_json}; </script> <script>(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',intercomSettings);}else{var d=document;var i=function(){i.c(arguments)};i.q=[];i.c=function(args){i.q.push(args)};w.Intercom=i;function l(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='#{Config.library_url || "https://widget.intercom.io/widget/#{app_id}"}';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);}if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}};})()</script> INTERCOM_SCRIPT str.respond_to?(:html_safe) ? str.html_safe : str end |
#valid? ⇒ Boolean
29 30 31 32 33 34 35 |
# File 'lib/intercom-rails/script_tag.rb', line 29 def valid? valid = user_details[:app_id].present? unless @show_everywhere valid = valid && (user_details[:user_id] || user_details[:email]).present? end valid end |