Skip to content

Commit

Permalink
Merge pull request #30 from hyoshida/refactor-theme-methods
Browse files Browse the repository at this point in the history
Refactor Theme's methods
  • Loading branch information
YOSHIDA Cake committed Sep 16, 2015
2 parents d2c1c7c + a5aeff6 commit d894d93
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ class Dispatcher
new Variant
when 'navigations:new', 'navigations:show', 'navigations:edit', 'navigations:update', 'navigations:create'
new Navigation
when 'themes:tree'
new ThemeTree
when 'themes:show_file'
new ThemeTree
new ThemeEditor
117 changes: 53 additions & 64 deletions backend/app/assets/javascripts/comable/admin/themes.coffee
Original file line number Diff line number Diff line change
@@ -1,64 +1,53 @@
can_use_comable_theme_editor = ->
return false unless $('#comable-theme-editor').length
return false unless $('#comable-theme-editor-form').length
true

can_use_comable_file_tree = ->
return false unless $('#comable-file-tree').length
true

comable_theme_editor_window = ->
editor_element = $('#comable-theme-editor').find('.comable-theme-editor-window').get(0)
ace.edit(editor_element)

initializa_comable_theme_editor = ->
editor = comable_theme_editor_window()
editor.setTheme('ace/theme/monokai')
editor.session.setMode('ace/mode/liquid')
$(window).bind('beforeunload', ->
window.beforeunload_message unless editor.session.getUndoManager().isClean()
)
$(document).on('page:before-change', ->
confirm(window.beforeunload_message) unless editor.session.getUndoManager().isClean()
)

add_comable_theme_editor_form_event = ->
$form = $('#comable-theme-editor-form')
$form.submit(->
editor = comable_theme_editor_window()
text = editor.getValue()
$(this).find('[name=code]').val(text)
)

add_comable_file_tree_event = ->
$comable_file_tree = $('#comable-file-tree')
$comable_file_tree.find('a').click((event) ->
event.preventDefault()
path = $(this).attr('href')
page_before_change = jQuery.Event('page:before-change')
$(document).trigger(page_before_change)
Turbolinks.visit(path) unless page_before_change.isDefaultPrevented()
)

resize_forms_height = ->
header_height = parseInt($('.comable-page-body').css('padding-top'))
footer_height = $('footer').outerHeight(true)
main_height = $(window).height() - header_height - footer_height
$comable_file_tree = $('#comable-file-tree')
$comable_file_tree.css('height', main_height + 'px') if $comable_file_tree.length
$comable_theme_editor = $('#comable-theme-editor')
$comable_theme_editor.css('height', main_height + 'px') if $comable_theme_editor.length

$(document).ready(->
if can_use_comable_theme_editor()
initializa_comable_theme_editor()
add_comable_theme_editor_form_event()
if can_use_comable_file_tree()
resize_forms_height()
add_comable_file_tree_event()
)

$(window).resize(->
return unless can_use_comable_file_tree()
resize_forms_height()
)
class @ThemeTree
constructor: ->
$(window).resize(@resize_forms_height)
@resize_forms_height()
@add_comable_file_tree_event()

add_comable_file_tree_event: ->
$comable_file_tree = $('#comable-file-tree')
$comable_file_tree.find('a').click((event) ->
event.preventDefault()
path = $(this).attr('href')
page_before_change = jQuery.Event('page:before-change')
$(document).trigger(page_before_change)
Turbolinks.visit(path) unless page_before_change.isDefaultPrevented()
)

resize_forms_height: ->
header_height = parseInt($('.comable-page-body').css('padding-top'))
footer_height = $('footer').outerHeight(true)
main_height = $(window).height() - header_height - footer_height
$comable_file_tree = $('#comable-file-tree')
$comable_file_tree.css('height', main_height + 'px') if $comable_file_tree.length
$comable_theme_editor = $('#comable-theme-editor')
$comable_theme_editor.css('height', main_height + 'px') if $comable_theme_editor.length

class @ThemeEditor
constructor: ->
@initializa_comable_theme_editor()
@add_comable_theme_editor_form_event()

initializa_comable_theme_editor: ->
editor = @comable_theme_editor_window()
editor.setTheme('ace/theme/monokai')
editor.session.setMode('ace/mode/liquid')
$(window).bind('beforeunload', ->
window.beforeunload_message unless editor.session.getUndoManager().isClean()
)
$(document).on('page:before-change', ->
confirm(window.beforeunload_message) unless editor.session.getUndoManager().isClean()
)

add_comable_theme_editor_form_event: ->
_this = @
$form = $('#comable-theme-editor-form')
$form.submit( ->
editor = _this.comable_theme_editor_window()
text = editor.getValue()
$(this).find('[name=code]').val(text)
)

comable_theme_editor_window: ->
editor_element = $('#comable-theme-editor').find('.comable-theme-editor-window').get(0)
ace.edit(editor_element)
14 changes: 5 additions & 9 deletions backend/app/controllers/comable/admin/themes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def update

def destroy
@theme.destroy
FileUtils.rm_rf(theme_dir)
@theme.dir.rmtree if @theme.dir.exist?
redirect_to comable.admin_themes_path, notice: Comable.t('successful')
end

Expand All @@ -51,7 +51,7 @@ def tree
end

def show_file
@code = File.read(filepath) if filepath && File.exist?(filepath)
@code = filepath.read if filepath.try(:file?)
end

def update_file
Expand All @@ -74,17 +74,13 @@ def save_file
# Validate the Liquid syntax
Liquid::Template.parse(params[:code])

FileUtils.mkdir_p(File.dirname(filepath)) unless File.exist?(filepath)
File.write(filepath, params[:code])
end

def theme_dir
File.join('themes', @theme.name)
filepath.dirname.mkpath unless filepath.dirname.exist?
filepath.write(params[:code])
end

def filepath
return unless params[:path]
File.join(theme_dir, params[:path])
@theme.dir + params[:path]
end

def theme_params
Expand Down
33 changes: 25 additions & 8 deletions backend/spec/controllers/comable/admin/themes_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,11 @@
describe 'GET show_file' do
let!(:theme) { create(:theme) }
let(:path) { 'path/to/file' }
let(:fullpath) { File.join('themes', theme.name, path) }
let(:filepath) { theme.dir + path }

before { allow(File).to receive(:exist?).with(fullpath).and_return(true) }
before { allow(File).to receive(:read).with(fullpath).and_return('sample code!') }
before { allow(subject).to receive(:filepath).and_return(filepath) }
before { allow(filepath).to receive(:file?).and_return(true) }
before { allow(filepath).to receive(:read).and_return('sample code!') }

it 'assigns the source code as @code' do
get :show_file, id: theme.to_param, path: path
Expand Down Expand Up @@ -202,6 +203,19 @@
end
end

describe '#filepath' do
it 'returns Pathname when params[:path] is exists' do
subject.instance_variable_set(:@theme, build(:theme))
allow(subject).to receive(:params).and_return(path: 'path/to/file')
expect(subject.send(:filepath)).to be_a(Pathname)
end

it 'returns nil when params[:path] is not exists' do
allow(subject).to receive(:params).and_return(path: nil)
expect(subject.send(:filepath)).to eq(nil)
end
end

describe '#save_file' do
it 'validates syntax of the liquid code' do
allow(subject).to receive(:params).and_return(code: <<-CODE)
Expand All @@ -216,14 +230,17 @@
theme = create(:theme)
code = 'sample code!'
path = 'path/to/file'
fullpath = File.join('themes', theme.name, path)
filepath = theme.dir + path
dirname = filepath.dirname

subject.instance_variable_set(:@theme, theme)
allow(subject).to receive(:params).and_return(path: path, code: code)
allow(subject).to receive(:params).and_return(code: code)
allow(subject).to receive(:filepath).and_return(filepath)
allow(filepath).to receive(:dirname).and_return(dirname)

expect(FileUtils).to receive(:mkdir_p).with(File.dirname(fullpath)).and_return(true)
expect(File).to receive(:exist?).with(fullpath).and_return(false)
expect(File).to receive(:write).with(fullpath, code).and_return(true)
expect(filepath.dirname).to receive(:exist?).and_return(false)
expect(filepath.dirname).to receive(:mkpath).and_return(true)
expect(filepath).to receive(:write).with(code).and_return(true)

subject.send(:save_file)
end
Expand Down
2 changes: 0 additions & 2 deletions core/app/models/comable/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ class Store < ActiveRecord::Base

liquid_methods :name, :meta_keywords, :meta_description, :email

delegate :name, to: :theme, prefix: true, allow_nil: true

class << self
def instance
first || new(name: default_name)
Expand Down
10 changes: 10 additions & 0 deletions core/app/models/comable/theme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ class Theme < ActiveRecord::Base
validates :homepage, length: { maximum: 255 }
validates :author, length: { maximum: 255 }

class << self
def dir
Rails.root.join('themes')
end
end

def dir
self.class.dir + name
end

def default_version
'0.1.0'
end
Expand Down
14 changes: 14 additions & 0 deletions core/spec/models/comable/theme_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@
it { is_expected.to validate_length_of(:homepage).is_at_most(255) }
it { is_expected.to validate_length_of(:author).is_at_most(255) }

describe '.dir' do
it 'returns the dirctory path for themes' do
expect(described_class.dir).to eq(Rails.root.join('themes'))
end
end

describe '#dir' do
it 'returns the dirctory path of the theme' do
name = 'example'
subject.name = name
expect(subject.dir).to eq(Rails.root.join('themes', name))
end
end

describe '#default_version' do
it 'returns the string' do
expect(subject.default_version).to be_a(String)
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/controllers/comable/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ApplicationController < ActionController::Base
before_filter :set_view_path

def set_view_path
prepend_view_path "themes/#{current_store.theme_name}" if current_store.theme_name
prepend_view_path current_store.theme.dir if current_store.theme
end
end
end

0 comments on commit d894d93

Please sign in to comment.