Permalink
Browse files

[#566] Inline Ckeditor

* Can update Htmlblocks using inline editing now
* Page status/publish button are updated after saving.
  • Loading branch information...
1 parent 5347761 commit 7dcfc59c9b540caaccca1fdbfe4a3ca439370b07 @peakpg peakpg committed Feb 21, 2013
View
@@ -34,7 +34,7 @@ group :test do
gem 'm', '~> 1.2'
gem 'factory_girl_rails', '3.3.0'
- gem "mocha"
+ gem "mocha", :require=>false
gem "sqlite3-ruby", :require => "sqlite3"
# Cucumber and dependencies
@@ -38,10 +38,23 @@ $(function () {
blur:function (event) {
var block_id = event.editor.name;
var block = $("#" + block_id);
- console.log("Saving " + block_id);
- console.log("Content Type: " + $("#" + block_id).data('class'));
+ var attribute = block.data('attribute');
+ var content_name = block.data('content-name');
+ var content_id = block.data('id');
var data = event.editor.getData();
- console.log("The following content should be saved.\n" + data);
+ var message = {
+ page_id: block.data('page-id'),
+ content: {}
+ };
+ message["content"][attribute] = data;
+ $.ajax({
+ url: '/cms/inline_content/' + content_name + "/" + content_id,
+ type: 'PUT',
+ data: message,
+ success: function(result) {
+ eval(result);
+ }
+ });
}
}
});
@@ -1,4 +1,4 @@
module Cms
-class HtmlBlocksController < Cms::ContentBlockController
-end
+ class HtmlBlocksController < Cms::ContentBlockController
+ end
end
@@ -0,0 +1,21 @@
+module Cms
+ class InlineContentController < Cms::BaseController
+ respond_to :json
+
+ def update
+ content = Content.find(params[:content_name], params[:id])
+ content.update_attributes(params[:content])
+ @page = Page.find_draft(params[:page_id])
+ if (!@page.live?)
+ @page_status = "label label-important"
+ @status_label = "DRAFT"
+ @enable_publish = true
+ else
+ @page_status = "label label-success"
+ @status_label = "Published"
+ @enable_publish = false
+ end
+ render layout: false
+ end
+ end
+end
@@ -28,9 +28,10 @@ def show(method, options={})
class: 'content-block',
contenteditable: true,
data: {
- class: @content_block.content_block_type,
+ content_name: @content_block.content_name,
id: @content_block.id,
- attribute: method
+ attribute: method,
+ page_id: @page.id
} do
content = @content_block.send(method)
View
@@ -0,0 +1,17 @@
+module Cms
+ # An API for interacting with the Content API of BrowserCMS.
+ module Content
+
+ # Find a single content block.
+ # example:
+ # Cms::Content.find("html_block", 12) # Finds a Cms::HtmlBlock with an id of 12.
+ #
+ # @param [String] content_name The name of the content type to find.
+ # @param [Integer] id The id of the content.
+ # @return [ContentBlock] A single content block
+ def self.find(content_type, id)
+ type = ContentType.find_by_key(content_type)
+ type.model_class.find(id)
+ end
+ end
+end
View
@@ -69,7 +69,6 @@ def actual_path
has_one :section_node, :as => :node, :dependent => :destroy, :inverse_of => :node, :class_name => 'Cms::SectionNode'
-
include Cms::Addressable
include Cms::Addressable::DeprecatedPageAccessors
@@ -83,6 +82,15 @@ def actual_path
validates_uniqueness_of :path, :scope=>:deleted
validate :path_not_reserved
+ # Find the latest draft of a given page.
+ #
+ # @param [Integer] id The id of the page
+ # @return [Cms::Page::Version] The version of the page as of the current Draft
+ def self.find_draft(id)
+ current = self.find(id)
+ current.as_of_draft_version
+ end
+
# Implements Versioning Callback.
def after_build_new_version(new_version)
copy_connectors(
@@ -0,0 +1,8 @@
+var status_label = $('#page-status-label', window.parent.document);
+status_label.addClass("<%= @page_status %>");
+status_label.html("<%= @status_label %>");
+
+var publish_button = $('#publish_button', window.parent.document);
+if(<%= @enable_publish %>){
+ publish_button.removeClass('disabled');
+}
@@ -0,0 +1,76 @@
+<span id="page-status-label">
+<% if !@page.live? %>
+ <span id="page-status-label" class="label label-important">DRAFT</span>
+<% else %>
+ <span id="page-status-label" class="label label-success">Published</span>
+<% end %>
+</span>
+<%= menu_button "Publish",
+ @page.live? ? '#' : cms.publish_page_path(@page),
+ :id => "publish_button",
+ :pull => 'left',
+ :method => :put,
+ :target => "_top",
+ :enabled => !(!current_user.able_to?(:publish_content) || !current_user.able_to_edit?(@page) || @page.version != @page.draft.version || @page.live?) %>
+<div class="btn-group pull-left">
+ <%= menu_button "Assign",
+ cms.new_page_task_path(@page),
+ :id => "assign_button",
+ :target => "_top",
+ :align => 'none',
+ :enabled => !(@page.assigned_to == current_user || !current_user.able_to_edit?(@page))
+ %>
+
+ <%= menu_button "Complete Task",
+ @page.current_task ? cms.complete_task_path(@page.current_task) : '#',
+ :id => "complete_task_button",
+ :method => :put,
+ :target => "_top",
+ :pull => 'center',
+ :enabled => !(@page.assigned_to != current_user || !current_user.able_to_edit?(@page)) %>
+</div>
+<%
+ tooltip = <<HTML
+ <b>Page Name</b>: #{@page.name}<br>
+ <b>Status</b>: #{@page.status}<br>
+ <b>Template</b>: #{@page.template_name}<br />
+ <b>Section</b>: #{@page.section.name}<br />
+ <b>Version</b>: #{@page.version} #{@page.live_version && @page.version == @page.live_version.version ? '(LIVE)' : ''}<br />
+
+HTML
+
+%>
+<%= menu_button "Edit Properties",
+ [:edit, @page],
+ :id => "edit_properties_button",
+ :target => "_top",
+ :pull => 'left',
+ :title => "Page Properties",
+ 'data-content'=>tooltip,
+ :enabled => current_user.able_to_edit?(@page)
+%>
+
+<%= menu_button "List Versions",
+ cms.versions_page_path(@page),
+ :target => "_top",
+ :pull => 'left',
+ :enabled => current_user.able_to_edit?(@page) %>
+
+<%= able_to? :publish_content do %>
+ <% if @page.draft_version? %>
+ <%= menu_button "Delete",
+ [@page],
+ :id => "delete_button",
+ :title => "Are you sure you want to delete '#{@page.name}'?",
+ :target => "_top",
+ :enabled => current_user.able_to_publish?(@page),
+ :pull => 'left',
+ :class => %w{confirm_with_title http_delete} %>
+ <% else %>
+ <%= menu_button "Revert to this Version",
+ cms.revert_page_path(@page, @page.version),
+ :id => "revert_button",
+ :method => :put,
+ :target => "_top" %>
+ <% end %>
+<% end %>
@@ -1,78 +1,7 @@
<%= content_for :button_bar do %>
- <% if !@page.live? %>
- <span class="label label-important">DRAFT</span>
- <% else %>
- <span class="label label-success">Published</span>
- <% end %>
- <%= menu_button "Publish",
- @page.live? ? '#' : cms.publish_page_path(@page),
- :id => "publish_button",
- :pull => 'left',
- :method => :put,
- :target => "_top",
- :enabled => !(!current_user.able_to?(:publish_content) || !current_user.able_to_edit?(@page) || @page.version != @page.draft.version || @page.live?) %>
- <div class="btn-group pull-left">
- <%= menu_button "Assign",
- cms.new_page_task_path(@page),
- :id => "assign_button",
- :target => "_top",
- :align => 'none',
- :enabled => !(@page.assigned_to == current_user || !current_user.able_to_edit?(@page))
- %>
-
- <%= menu_button "Complete Task",
- @page.current_task ? cms.complete_task_path(@page.current_task) : '#',
- :id => "complete_task_button",
- :method => :put,
- :target => "_top",
- :pull => 'center',
- :enabled => !(@page.assigned_to != current_user || !current_user.able_to_edit?(@page)) %>
- </div>
- <%
- tooltip = <<HTML
- <b>Page Name</b>: #{@page.name}<br>
- <b>Status</b>: #{@page.status}<br>
- <b>Template</b>: #{@page.template_name}<br />
- <b>Section</b>: #{@page.section.name}<br />
- <b>Version</b>: #{@page.version} #{@page.live_version && @page.version == @page.live_version.version ? '(LIVE)' : ''}<br />
-
-HTML
-
- %>
- <%= menu_button "Edit Properties",
- [:edit, @page],
- :id => "edit_properties_button",
- :target => "_top",
- :pull => 'left',
- :title => "Page Properties",
- 'data-content'=>tooltip,
- :enabled => current_user.able_to_edit?(@page)
- %>
-
- <%= menu_button "List Versions",
- cms.versions_page_path(@page),
- :target => "_top",
- :pull => 'left',
- :enabled => current_user.able_to_edit?(@page) %>
-
- <%= able_to? :publish_content do %>
- <% if @page.draft_version? %>
- <%= menu_button "Delete",
- [@page],
- :id => "delete_button",
- :title => "Are you sure you want to delete '#{@page.name}'?",
- :target => "_top",
- :enabled => current_user.able_to_publish?(@page),
- :pull => 'left',
- :class => %w{confirm_with_title http_delete} %>
- <% else %>
- <%= menu_button "Revert to this Version",
- cms.revert_page_path(@page, @page.version),
- :id => "revert_button",
- :method => :put,
- :target => "_top" %>
- <% end %>
- <% end %>
+ <span id="edit_page_buttons">
+ <%= render "cms/pages/page_buttons" %>
+ </span>
<%# @TODO - Extract this into a partial. I don't really like having this function embedded into the toolbar either. %>
<%= link_to "Test Modal", "#modal-add-content", :data => { :toggle => 'modal'}, :class=>"btn btn-primary" %>
<div id="modal-add-content" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
View
@@ -15,6 +15,7 @@
match '/toolbar', :to=>"toolbar#index", :as=>'toolbar'
match '/content_types', :to=>"content_types#index", :as=>'content_types'
+ put "/inline_content/:content_name/:id", to: "inline_content#update", as: "update_inline_content"
resources :page_components
resources :connectors do
member do
@@ -44,6 +44,12 @@ def display_name_plural
end
module InstanceMethods
+ # Returns a machine readable key that identifies the type of content this is.
+ # Should match the key passed to ContentType.find_by_key
+ def content_name
+ self.class.name.demodulize.underscore
+ end
+
def connected_pages
return @connected_pages if @connected_pages
@connected_pages = Page.connected_to(self)
@@ -264,6 +264,11 @@ def save!(perform_validations=true)
save(:validate => perform_validations) || raise(ActiveRecord::RecordNotSaved.new(errors.full_messages))
end
+ # Returns the most recently created Version for this class. Drafts are the most recent change from
+ # the _versions table for a given content item.
+ # i.e. For Cms::Page, this would return Cms::Page::Version
+ #
+ # @return [<Class>::Version] The version for this class that represents the draft.
def draft
versions.first(:order => "version desc")
end
View
@@ -0,0 +1,2 @@
+## Issue #566: Add Ckeditor inline editing
+
@@ -1,5 +1,5 @@
# This removes all the Sprocket asset logging from development log, which makes them much saner.
-silence_sprockets = true
+silence_sprockets = false
if silence_sprockets
Rails.application.assets.logger = Logger.new('/dev/null')
View
@@ -4,6 +4,10 @@
require "minitest/autorun"
require "minitest/rails"
+Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
+require 'factories/factories'
+require 'factories/attachable_factories'
+
#require 'minitest/reporters'
#MiniTest::Reporters.use!
@@ -13,9 +17,21 @@
# Uncomment if you want awesome colorful output
# require "minitest/pride"
+require 'database_cleaner'
+DatabaseCleaner.strategy = :truncation
+
+class MiniTest::Spec
+ after :each do
+ DatabaseCleaner.clean
+ end
+ include FactoryGirl::Syntax::Methods
+ include FactoryHelpers
+end
+
class MiniTest::Rails::ActiveSupport::TestCase
+
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
- fixtures :all
+ #fixtures :all
# Add more helper methods to be used by all tests here...
end
View
@@ -8,6 +8,7 @@
# Load support files
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
+require 'mocha/setup'
require 'action_view/test_case'
# Allows Generators to be unit tested
@@ -0,0 +1,12 @@
+require "minitest_helper"
+
+describe "ContentApi" do
+
+ let(:html_block){ Cms::HtmlBlock.new }
+
+ describe '#content_name' do
+ it "should generate a readable key" do
+ html_block.content_name.must_equal "html_block"
+ end
+ end
+end
@@ -79,8 +79,7 @@ def setup
end
test "#build_object_from_version recreates a block based on a given version record" do
- html_version = Cms::HtmlBlock::Version.first
-
+ html_version = @published_block.versions[0]
html = html_version.build_object_from_version
assert_equal "Version 1", html.name
Oops, something went wrong.

0 comments on commit 7dcfc59

Please sign in to comment.