From a2308787f1a4eda513b2618799f634050dc164d4 Mon Sep 17 00:00:00 2001 From: Didier Lafforgue Date: Tue, 28 Feb 2012 11:25:51 +0100 Subject: [PATCH] fix theme assets tests --- .../views/theme_assets/_form_view.js.coffee | 2 +- .../views/theme_assets/edit_view.js.coffee | 9 +++++++- app/models/locomotive/theme_asset.rb | 4 +++- .../locomotive/theme_asset_presenter.rb | 13 ++++++++++-- features/backoffice/theme_assets.feature | 21 +++++++++---------- .../step_definitions/theme_asset_steps.rb | 11 +++++----- 6 files changed, 38 insertions(+), 22 deletions(-) diff --git a/app/assets/javascripts/locomotive/views/theme_assets/_form_view.js.coffee b/app/assets/javascripts/locomotive/views/theme_assets/_form_view.js.coffee index ef57a797..57ce55ee 100644 --- a/app/assets/javascripts/locomotive/views/theme_assets/_form_view.js.coffee +++ b/app/assets/javascripts/locomotive/views/theme_assets/_form_view.js.coffee @@ -49,7 +49,7 @@ class Locomotive.Views.ThemeAssets.FormView extends Locomotive.Views.Shared.Form @$('input#theme_asset_performing_plain_text').val(false) else @$('#file-selector').slideUp 'normal', => - @$('#text-selector').slideDown() + @$('#text-selector').slideDown 'normal', => @editor.refresh() @model.set(performing_plain_text: true) @$('#theme_asset_performing_plain_text').val(true) diff --git a/app/assets/javascripts/locomotive/views/theme_assets/edit_view.js.coffee b/app/assets/javascripts/locomotive/views/theme_assets/edit_view.js.coffee index 81fdf639..a14bbe0f 100644 --- a/app/assets/javascripts/locomotive/views/theme_assets/edit_view.js.coffee +++ b/app/assets/javascripts/locomotive/views/theme_assets/edit_view.js.coffee @@ -4,6 +4,13 @@ class Locomotive.Views.ThemeAssets.EditView extends Locomotive.Views.ThemeAssets save: (event) -> @save_in_ajax event, on_success: (response, xhr) => + window.response = response + window.xhr = xhr help = @$('.inner > p.help') help.find('b').html(response.dimensions) - help.find('a').html(response.url).attr('href', response.url) \ No newline at end of file + help.find('a').html(response.url).attr('href', response.url) + + window.editor = @editor + + if response.plain_text? + @editor.setValue response.plain_text diff --git a/app/models/locomotive/theme_asset.rb b/app/models/locomotive/theme_asset.rb index f87b7f16..aac9fa02 100644 --- a/app/models/locomotive/theme_asset.rb +++ b/app/models/locomotive/theme_asset.rb @@ -99,6 +99,8 @@ module Locomotive :tempfile => StringIO.new(sanitized_source), :filename => "#{self.plain_text_name}.#{self.stylesheet? ? 'css' : 'js'}" }) + + @plain_text = sanitized_source # no need to reset the plain_text instance variable to have the last version end def to_liquid @@ -106,7 +108,7 @@ module Locomotive end def as_json(options = {}) - Locomotive::ThemeAssetPresenter.new(self).as_json + Locomotive::ThemeAssetPresenter.new(self, options).as_json end def self.all_grouped_by_folder(site) diff --git a/app/presenters/locomotive/theme_asset_presenter.rb b/app/presenters/locomotive/theme_asset_presenter.rb index 7f07c184..929204ba 100644 --- a/app/presenters/locomotive/theme_asset_presenter.rb +++ b/app/presenters/locomotive/theme_asset_presenter.rb @@ -1,7 +1,7 @@ module Locomotive class ThemeAssetPresenter < BasePresenter - delegate :content_type, :folder, :to => :source + delegate :content_type, :folder, :plain_text, :to => :source def local_path self.source.local_path(true) @@ -24,7 +24,16 @@ module Locomotive end def included_methods - super + %w(content_type folder local_path url size dimensions updated_at) + default_list = %w(content_type folder local_path url size dimensions updated_at) + default_list += %w(plain_text) if plain_text? + super + default_list + end + + private + + def plain_text? + # FIXME: self.options contains all the options passed by the responder + self.options[:template] == 'update' && self.source.errors.empty? && self.source.stylesheet_or_javascript? end end diff --git a/features/backoffice/theme_assets.feature b/features/backoffice/theme_assets.feature index 194882f8..8d556079 100644 --- a/features/backoffice/theme_assets.feature +++ b/features/backoffice/theme_assets.feature @@ -35,12 +35,12 @@ Scenario: Updating a stylesheet Given a stylesheet asset named "application" When I go to theme assets And I follow "application.css" - And I fill in "theme_asset[plain_text]" with "Lorem ipsum (updated)" + And I change the theme asset code to "Lorem ipsum (updated)" And I press "Save" Then I should see "File was successfully updated." And I should see "Editing application.css" And I should see "application.css" - And I should see "Lorem ipsum (updated)" as theme asset code + And I should see "Lorem ipsum (updated)" as the theme asset code Scenario: Uploading a javascript When I go to theme assets @@ -52,15 +52,14 @@ Scenario: Uploading a javascript And I should see "Code" And I should see "javascripts/test/application.js" -# @javascript -# Scenario: Updating a javascript -# Given a javascript asset named "application" -# When I go to theme assets -# And I follow "application.js" -# And I change the asset code with -# And I fill in "theme_asset[plain_text]" with "Lorem ipsum (updated)" -# And I press "Save" -# Then I should see "File was successfully updated." +@javascript +Scenario: Updating a javascript + Given a javascript asset named "application" + When I go to theme assets + And I follow "application.js" + And I change the theme asset code to "Lorem ipsum (updated)" + And I press "Save" + Then I should see "File was successfully updated." Scenario: Uploading an image which already exists When I go to theme assets diff --git a/features/step_definitions/theme_asset_steps.rb b/features/step_definitions/theme_asset_steps.rb index 1ad619e6..d23614e1 100644 --- a/features/step_definitions/theme_asset_steps.rb +++ b/features/step_definitions/theme_asset_steps.rb @@ -34,13 +34,12 @@ end # other stuff # change the template -# When /^I change the theme asset code to "([^"]*)"$/ do |page_template| -# page.evaluate_script "window.application_view.view.model.set({ 'raw_template': '#{page_template}' })" -# end +When /^I change the theme asset code to "([^"]*)"$/ do |plain_text| + page.evaluate_script "window.application_view.view.editor.setValue('#{plain_text}')" +end - -Then /^I should see "([^"]*)" as theme asset code$/ do |code| - find(:css, "#theme_asset_plain_text").text.should == code +Then /^I should see "([^"]*)" as the theme asset code$/ do |code| + find(:css, "#theme_asset_plain_text").value.should == code end Then /^I should see a delete image button$/ do