module Sorry::Rails::ScriptTagHelper

An ActionView helper which generates the JavaScript includes required for the plugin to work.

Public Instance Methods

sorry_script_include_tag(options) click to toggle source
# File lib/sorry/rails/script_tag_helper.rb, line 32
def sorry_script_include_tag(options)
    # Build the JavaScript tag for the plugin include.
    # Use the latest JS version defined in the plugin.
    javascript_include_tag "https://code.sorryapp.com/status-bar/#{Sorry::Rails::PLUGIN_VERSION}/status-bar.min.js",
                           # Define the pages identity.
                           data: { for: options.fetch('page_id') },
                           # Load asynchronously.
                           async: true
end
sorry_script_payload_tag(options) click to toggle source
# File lib/sorry/rails/script_tag_helper.rb, line 42
def sorry_script_payload_tag(options)
    # Get the method name
    current_user_method = options.fetch('current_user_method')

    # See if the current user is signed in, so we can
    # include them as a subscriber.
    if respond_to?(current_user_method) && send(current_user_method).present?
        # Get the current user.
        current_request_user = send(current_user_method)

        # Serialize the user into a subscriber payload.
        subscriber_payload = SubscriberSerializer.new(current_request_user).to_json

        # We have a user method, let's include the JS payload
        # object for them as a subscriber.
        javascript_tag id: 'sorry-subscriber-data' do
            # Include the subscriber payload on the window.
            "window.SorryAPIOptions = { \"subscriber\": #{subscriber_payload} };".html_safe
        end
    end
end
sorry_script_tag(options = {}) click to toggle source

Generate the Sorry Website Plugin script tag to display status notices to the user and register them as a subscriber.

# File lib/sorry/rails/script_tag_helper.rb, line 23
def sorry_script_tag(options = {})
    # Merge configuration in options.
    options.reverse_merge!(Sorry::Rails.configuration)

    # Include the payload tag and the include
    # tags for the plugin.
    safe_join([sorry_script_payload_tag(options), sorry_script_include_tag(options)])
end