Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Commit

Permalink
Simplify JS init
Browse files Browse the repository at this point in the history
There is no controller specific code
  • Loading branch information
ledermann committed Nov 5, 2017
1 parent 8048241 commit 2977f06
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 30 deletions.
13 changes: 0 additions & 13 deletions app/assets/javascripts/init.coffee
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
# Based on the blog post "Turbolinks — How to organize your Javascript code"
# https://medium.com/@coorasse/turbolinks-how-to-organize-your-javascript-code-df83d59c0886

class @Init
constructor: ->
@startup_modules()
@startup_controller_specific_code()

startup_modules: ->
new AhoyTracker()

# Forms
Expand All @@ -21,11 +14,5 @@ class @Init
new TooltipEnabler()
new Autocomplete()

startup_controller_specific_code: ->
klass_name = $('body').data('js-class')
if 'function' is typeof window[klass_name]
klass = window[klass_name]
new klass()

$(document).on 'turbolinks:load', ->
new Init()
4 changes: 0 additions & 4 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ class ApplicationController < ActionController::Base

protect_from_forgery with: :exception

helper_method def body_js_class
@body_js_class ||= [ controller_name.camelcase, action_name.camelcase ].join
end

helper_method def title(value = nil)
if value
@title = value
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ html[lang="en"]
= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload'
= javascript_include_tag 'application', 'data-turbolinks-track': 'reload'

body data-js-class=body_js_class
body
= render 'layouts/navbar'

#main
Expand Down
7 changes: 0 additions & 7 deletions spec/controllers/application_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ def index
end
end

describe '#body_js_class' do
it "should named by controller_name and action_name" do
get :index
expect(controller.body_js_class).to eq('DummiesIndex')
end
end

describe '#title' do
it 'returns nil per default' do
expect(subject.title).to eq(nil)
Expand Down
5 changes: 0 additions & 5 deletions spec/views/layouts/application.html.slim_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
describe 'layouts/application' do
before :each do
without_partial_double_verification do
allow(view).to receive(:body_js_class).and_return('ControllerAction')
allow(view).to receive(:title).and_return('We have a title')
end
render
end

it "should render body tag with data attribute" do
expect(rendered).to have_selector('body[data-js-class=ControllerAction]')
end

it "should render title tag" do
expect(rendered).to have_title('We have a title :: DockerRails')
end
Expand Down

0 comments on commit 2977f06

Please sign in to comment.