class SeedTray
    constructor: () ->
        if window.Turbolinks != undefined
            $(window).on "page:change", => @ready()
        else
            $(@ready)
        @root = <%= Rails.application.class.to_s.split('::').first %>

    # Dynamically delegate ready to controller#action specific ready methods
    ready: =>
        #  Run the page specific ready code
        @delegate_ready()

        # Run the code that should run on every page.
        @site_wide_ready()

    delegate_ready: =>
        controller = $("[data-controller]").data("controller")
        action = $("[data-action").data("action")

        if @root[controller] && ready_defined(@root[controller])
            @root[controller].ready()
        else
            console.info "Skipped #{@root.name}.#{controller}.ready()."

        if @root[controller] && @root[controller][action] && ready_defined(@root[controller][action])
            @root[controller][action].ready()
            console.info "Executed #{@root.name}.#{controller}.#{action}.ready()."
        else
            console.info "Skipped #{@root.name}.#{controller}.#{action}.ready()."

    ready_defined: (object) ->
        object.ready != undefined

    site_wide_ready: ->

<%= Rails.application.class.to_s.split('::').first %>.delegator = new SeedTray()

<% app_name = Rails.application.class.to_s.split('::').first %>

class SeedTray
    constructor: () ->
        if window.Turbolinks != undefined
            $(window).on "page:change", => @ready()
        else
            $(@ready)
        @root = <%= app_name %>

    # Dynamically delegate ready to controller#action specific ready methods
    ready: =>
        #  Run the page specific ready code
        @delegate_ready()

        # Run the code that should run on every page.
        @site_wide_ready()

    delegate_ready: =>
        controller = $("[data-controller]").data("controller")
        action = $("[data-action").data("action")

        if @root[controller] && @ready_defined(@root[controller])
            @root[controller].ready()
        else
            console.info "Skipped #{@root.name}.#{controller}.ready()."

        if @root[controller] && @root[controller][action] && @ready_defined(@root[controller][action])
            @root[controller][action].ready()
            console.info "Executed #{@root.name}.#{controller}.#{action}.ready()."
        else
            console.info "Skipped #{@root.name}.#{controller}.#{action}.ready()."

    ready_defined: (object) ->
        object.ready != undefined

    site_wide_ready: ->

<%= app_name %>.delegator = new SeedTray()

Refactor application name code generator
Refactor application name code generator
