Skip to content

Commit

Permalink
Allow any block to be in context edited.
Browse files Browse the repository at this point in the history
* Add a 'show' helper, valid for block templates.
* Show will make a field editable by mercury
  • Loading branch information
peakpg committed Mar 19, 2013
1 parent b412349 commit f92a42d
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 56 deletions.
91 changes: 47 additions & 44 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,31 @@ PATH
GEM
remote: http://rubygems.org/
specs:
actionmailer (3.2.8)
actionpack (= 3.2.8)
actionmailer (3.2.9)
actionpack (= 3.2.9)
mail (~> 2.4.4)
actionpack (3.2.8)
activemodel (= 3.2.8)
activesupport (= 3.2.8)
actionpack (3.2.9)
activemodel (= 3.2.9)
activesupport (= 3.2.9)
builder (~> 3.0.0)
erubis (~> 2.7.0)
journey (~> 1.0.4)
rack (~> 1.4.0)
rack-cache (~> 1.2)
rack-test (~> 0.6.1)
sprockets (~> 2.1.3)
activemodel (3.2.8)
activesupport (= 3.2.8)
sprockets (~> 2.2.1)
activemodel (3.2.9)
activesupport (= 3.2.9)
builder (~> 3.0.0)
activerecord (3.2.8)
activemodel (= 3.2.8)
activesupport (= 3.2.8)
activerecord (3.2.9)
activemodel (= 3.2.9)
activesupport (= 3.2.9)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activeresource (3.2.8)
activemodel (= 3.2.8)
activesupport (= 3.2.8)
activesupport (3.2.8)
activeresource (3.2.9)
activemodel (= 3.2.9)
activesupport (= 3.2.9)
activesupport (3.2.9)
i18n (~> 0.6)
multi_json (~> 1.0)
addressable (2.3.2)
Expand All @@ -51,15 +51,16 @@ GEM
ffi (>= 1.0.11)
rspec (>= 2.7.0)
bluecloth (2.2.0)
bootstrap-sass (2.1.0.1)
bootstrap-sass (2.2.1.1)
sass (~> 3.2)
builder (3.0.4)
capybara (1.1.2)
capybara (2.0.0)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
selenium-webdriver (~> 2.0)
xpath (~> 0.1.4)
xpath (~> 1.0.0)
childprocess (0.3.6)
ffi (~> 1.0, >= 1.0.6)
ckeditor_rails (3.6.4.1)
Expand Down Expand Up @@ -106,8 +107,8 @@ GEM
json (1.7.5)
launchy (2.1.2)
addressable (~> 2.3)
libwebsocket (0.1.5)
addressable
libwebsocket (0.1.6)
websocket
mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
Expand All @@ -118,7 +119,7 @@ GEM
mime-types (1.19)
mocha (0.9.8)
rake
multi_json (1.3.6)
multi_json (1.3.7)
mysql2 (0.3.11)
nokogiri (1.5.5)
paperclip (3.0.4)
Expand All @@ -135,46 +136,47 @@ GEM
rack
rack-test (0.6.2)
rack (>= 1.0)
rails (3.2.8)
actionmailer (= 3.2.8)
actionpack (= 3.2.8)
activerecord (= 3.2.8)
activeresource (= 3.2.8)
activesupport (= 3.2.8)
rails (3.2.9)
actionmailer (= 3.2.9)
actionpack (= 3.2.9)
activerecord (= 3.2.9)
activeresource (= 3.2.9)
activesupport (= 3.2.9)
bundler (~> 1.0)
railties (= 3.2.8)
railties (3.2.8)
actionpack (= 3.2.8)
activesupport (= 3.2.8)
railties (= 3.2.9)
railties (3.2.9)
actionpack (= 3.2.9)
activesupport (= 3.2.9)
rack-ssl (~> 1.3.2)
rake (>= 0.8.7)
rdoc (~> 3.4)
thor (>= 0.14.6, < 2.0)
rake (0.9.2.2)
rake (10.0.2)
rdoc (3.12)
json (~> 1.4)
rspec (2.11.0)
rspec-core (~> 2.11.0)
rspec-expectations (~> 2.11.0)
rspec-mocks (~> 2.11.0)
rspec-core (2.11.1)
rspec-expectations (2.11.3)
rspec (2.12.0)
rspec-core (~> 2.12.0)
rspec-expectations (~> 2.12.0)
rspec-mocks (~> 2.12.0)
rspec-core (2.12.0)
rspec-expectations (2.12.0)
diff-lcs (~> 1.1.3)
rspec-mocks (2.11.3)
rspec-mocks (2.12.0)
ruby-prof (0.11.2)
rubyzip (0.9.9)
sass (3.2.1)
sass (3.2.3)
sass-rails (3.2.5)
railties (~> 3.2.0)
sass (>= 3.1.10)
tilt (~> 1.3)
selenium-webdriver (2.25.0)
selenium-webdriver (2.26.0)
childprocess (>= 0.2.5)
libwebsocket (~> 0.1.3)
multi_json (~> 1.0)
rubyzip
sprockets (2.1.3)
sprockets (2.2.1)
hike (~> 1.2)
multi_json (~> 1.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.6)
Expand All @@ -191,11 +193,12 @@ GEM
treetop (1.4.12)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.34)
tzinfo (0.3.35)
uglifier (1.3.0)
execjs (>= 0.3.0)
multi_json (~> 1.0, >= 1.0.2)
xpath (0.1.4)
websocket (1.0.3)
xpath (1.0.0)
nokogiri (~> 1.3)
yard (0.8.3)

Expand Down
35 changes: 31 additions & 4 deletions app/helpers/cms/rendering_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,51 @@
module Cms
module RenderingHelper

# Renders the content for the given field from the current content block.
# Designed to be used in Block Templates instead of direct output of fields.
# Example:
# <pre><%= show :content %></pre>
# Instead of:
# <pre><%= @content_block.content.html_safe %></pre>
#
# Why bother?: This abstracts the actual variable name (makes future upgrades more robust),
# as well as let us mark up the content with html_safe,
# plus conditionally make fields editable.
#
# @param [Symbol] method
# @param [Hash] options
def show(method, options={})
if (!logged_in?) # Need to check the current user can edit the page attached to this block too
@content_block.send(method).html_safe
else
connectable = @content_block
editor_info = @content_block.editor_info(method)
content_tag editor_info[:element], id: "blocks[#{connectable.class}][#{connectable.id}][#{method}]", class: 'content-block',
data: {mercury: editor_info[:region] } do
content = @content_block.send(method)
content.to_s.html_safe
end
end

end

# Renders a table of attachments for a given content block.
# This is intended as a basic view of the content, and probably won't be suitable for blocks that need to be added directly to pages.
#
def attachment_viewer(content)
render :partial => 'cms/attachments/attachment_table', :locals => { :block => content, :can_delete => false }
render :partial => 'cms/attachments/attachment_table', :locals => {:block => content, :can_delete => false}
end

# Determines if a user is currently editing this page
def is_editing_page?(page)
logged_in? && current_user.able_to_edit?(page)
#logged_in? && @mode == "edit" && current_user.able_to_edit?(page)
end

def render_connector_and_connectable(connector, connectable)
logger.debug "Rendering #{connectable} "
if is_editing_page?(connector.page)
#render(:partial => 'cms/pages/edit_connector', :locals => { :connector => connector, :connectable => connectable})
render(:partial => 'cms/pages/edit_content', :locals => { :connector => connector, :connectable => connectable})
render(:partial => 'cms/pages/edit_content', :locals => {:connector => connector, :connectable => connectable})
else
render_connectable(connectable)
end
Expand Down
11 changes: 8 additions & 3 deletions app/models/cms/page_component.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module Cms

# Handles the conversion from Mercury editor (i.e. the JSON hash it submits) to a Cms Page and blocks
class PageComponent
extend ActiveModel::Naming

Expand All @@ -7,7 +9,7 @@ class PageComponent
def initialize(page_id, params)
params = HashWithIndifferentAccess.new(params)
self.page_title = params[:page_title]
self.blocks = params[:blocks]
self.blocks = params[:blocks] ? params[:blocks] : []
self.page_id = page_id
end

Expand All @@ -23,8 +25,11 @@ def save
content_ids.each do |block_id|
block = content_block_class.constantize.find(block_id)

# Only really handles HtmlBlock right now
block.content = block_type[1][block_id][:value]
block_attribute_names = block_type[1][block_id].keys
block_attribute_names.each do |attr_name|
# TODO: Mass assignment concern here...
block.send("#{attr_name}=".to_sym, block_type[1][block_id][attr_name][:value])
end
block.save!
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/cms/html_blocks/render.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= @content_block.content.html_safe %>
<%= show :content %>
3 changes: 1 addition & 2 deletions app/views/cms/pages/_edit_content.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<%= content_tag 'div', id: "blocks[#{connectable.class}][#{connectable.id}]", class: 'content-block',
data: { mercury: 'full',
move_up: cms.move_up_connector_path(connector, format: :json),
data: { move_up: cms.move_up_connector_path(connector, format: :json),
move_down: cms.move_down_connector_path(connector, format: :json),
remove: cms.connector_path(connector, format: :json)
} do %>
Expand Down
13 changes: 13 additions & 0 deletions lib/cms/behaviors/rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ def ivars_to_ignore

end
module InstanceMethods

# Returns the Mercury editor type for a given attribute
# @param [Symbol] method (i.e. :name, :content, etc)
# @return [Hash]
def editor_info(method)
column = self.class.columns_hash[method.to_s]
if column.type == :text
{:element => 'div', :region => 'full'}
else
{:element => 'span', :region => 'simple'}
end
end

def prepare_to_render(controller)
# Give this renderable a reference to the controller
@controller = controller
Expand Down
4 changes: 2 additions & 2 deletions test/dummy/app/views/cms/products/render.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p><b>Name:</b> <%= @content_block.name %></p>
<p><b>Price:</b> <%= @content_block.price %></p>
<p><b>Name:</b> <%= show :name %></p>
<p><b>Price:</b> <%= show :price %></p>
<p><b>Photo 1:</b> <%= tag :img,
:src => attachment_path_for(@content_block.photo_1),
:alt => h(@content_block.name),
Expand Down
23 changes: 23 additions & 0 deletions test/unit/behaviors/mercury_editor_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require "test_helper"

class MecuryEditorTest < ActiveSupport::TestCase

test "#editor_type_for Text Areas" do
field_info = Cms::HtmlBlock.new.editor_info(:content)
assert_equal "full", field_info[:region]
assert_equal "div", field_info[:element]
end

test "#editor_type_for text fields" do
field_info = content_block().editor_info(:name)
assert_equal "simple", field_info[:region]
assert_equal "span", field_info[:element]
end

private

# Factory for a simple content block.
def content_block
Cms::HtmlBlock.new
end
end
30 changes: 30 additions & 0 deletions test/unit/lib/cms/page_components_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,35 @@ def setup
assert c.save, "Should save"
assert_equal "New Title", Page.find(@page.id).draft.title
end

test "#saves name and content for an HtmlBlock" do
b = create(:html_block, name: "Old block name", content: "Old Content")
@page.add_content(b)
@page.save!
msg_payload = {
"blocks" => {
"Cms::HtmlBlock" => {
b.id => {
"content" => {"type" => "full", "value" => "New Content"},
"name" => {"type" => "simple", "value" => "New Name"}
}
}
}}
add_page_title(msg_payload)
c = PageComponent.new(@page.id, msg_payload)
c.save

updated_block = HtmlBlock.find(b.id).draft
assert_equal "New Name", updated_block.name
assert_equal "New Content", updated_block.content
end

private
# page_title is required. This is a pseudo factory for testing.
def add_page_title(msg)
msg["page_title"] = {"type" => "simple", "data" => {}, "value" => "New Title"}
end
end


end

0 comments on commit f92a42d

Please sign in to comment.