Skip to content

Commit

Permalink
fixing issue with cms_snippet_render helper context. closes #823
Browse files Browse the repository at this point in the history
  • Loading branch information
GBH committed Apr 30, 2018
1 parent 1795dbf commit 56c63e5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ComfortableMexicanSofa is a powerful Ruby on Rails 5.2+ CMS (Content Management
* CMS stays away from the rest of your application
* Powerful page templating capability using [Content Tags](https://github.com/comfy/comfortable-mexican-sofa/wiki/Docs:-Content-Tags)
* [Multiple Sites](https://github.com/comfy/comfortable-mexican-sofa/wiki/Docs:-Sites) from a single installation
* Multi-Language Support (i18n) (ca, cs, da, de, en, es, fr, gr, it, ja, nb, nl, pl, pt-BR, ru, sv, tr, uk, zh-CN, zh-TW) and page localization.
* Multi-Language Support (i18n) (ca, cs, da, de, en, es, fi, fr, gr, it, ja, nb, nl, pl, pt-BR, ru, sv, tr, uk, zh-CN, zh-TW) and page localization.
* [CMS Seeds](https://github.com/comfy/comfortable-mexican-sofa/wiki/Docs:-CMS-Seeds) for initial content population
* [Revision History](https://github.com/comfy/comfortable-mexican-sofa/wiki/Docs:-Revisions) to revert changes
* [Extendable Admin Area](https://github.com/comfy/comfortable-mexican-sofa/wiki/HowTo:-Reusing-Admin-Area) built with [Bootstrap 4](http://getbootstrap.com) (responsive design). Using [CodeMirror](http://codemirror.net) for HTML and Markdown highlighing and [Redactor](http://imperavi.com/redactor) as the WYSIWYG editor.
Expand Down
22 changes: 12 additions & 10 deletions app/helpers/comfy/cms_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,7 @@ def cms_fragment_render(identifier, page = @cms_page)
# Example:
# cms_snippet_content(:my_snippet)
def cms_snippet_content(identifier, cms_site = @cms_site)
unless cms_site
if respond_to?(:request) && request
host = request.host_with_port.downcase
path = request.fullpath
end
cms_site = Comfy::Cms::Site.find_site(host, path)
end
cms_site ||= cms_site_detect
snippet = cms_site&.snippets&.find_by_identifier(identifier)
return "" unless snippet
snippet.content
Expand All @@ -52,9 +46,17 @@ def cms_snippet_content(identifier, cms_site = @cms_site)
# Same as cms_snippet_content but cms tags will be expanded. Note that there
# is no page context, so snippet cannot contain fragment tags.
def cms_snippet_render(identifier, cms_site = @cms_site)
content = cms_snippet_content(identifier, cms_site)
r = ComfortableMexicanSofa::Content::Renderer.new(Comfy::Cms::Page.new)
render inline: r.render(r.nodes(r.tokenize(content)))
cms_site ||= cms_site_detect
snippet = cms_site&.snippets&.find_by_identifier(identifier)
return "" unless snippet
r = ComfortableMexicanSofa::Content::Renderer.new(snippet)
render inline: r.render(r.nodes(r.tokenize(snippet.content)))
end

def cms_site_detect
host = request.host_with_port.downcase
path = request.fullpath
Comfy::Cms::Site.find_site(host, path)
end

# Wrapper to deal with Kaminari vs WillPaginate
Expand Down
28 changes: 27 additions & 1 deletion test/helpers/cms_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def test_cms_snippet_content
assert_equal "", cms_snippet_content(:invalid)
end

def test_cms_snippet_content_with_site_loading
def test_cms_snippet_content_with_site_detection
@cms_site = nil
assert_equal "snippet content", cms_snippet_content(:default)
end
Expand All @@ -100,6 +100,12 @@ def test_cms_snippet_render
assert_equal "snippet content", cms_snippet_render(:default)
end

def test_cms_snippet_render_with_tags_and_context
file = comfy_cms_files(:default)
comfy_cms_snippets(:default).update_column(:content, "{{cms:file_link #{file.id}}}")
assert_equal rails_blob_path(file.attachment, only_path: true), cms_snippet_render(:default)
end

def test_cms_snippet_with_erb
comfy_cms_snippets(:default).update_column(:content, "<%= 1 + 1 %>")
assert_equal "&lt;%= 1 + 1 %&gt;", cms_snippet_render(:default)
Expand All @@ -110,4 +116,24 @@ def test_cms_snippet_render_with_tags
assert_equal "a hello b", cms_snippet_render(:default)
end

def test_cms_site_detect
site = comfy_cms_sites(:default)
site.update_column(:path, "/en")
assert_equal site, cms_site_detect

site_b = Comfy::Cms::Site.create!(
label: "with path",
identifier: "with-path",
hostname: site.hostname,
path: "fr"
)
request.fullpath = "/fr"
assert_equal site_b, cms_site_detect

site_b.update_columns(hostname: "site_b.com", path: "en")
request.host_with_port = "site_b.com"
request.fullpath = "/en"
assert_equal site_b, cms_site_detect
end

end
21 changes: 21 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,27 @@ def with_routing

class ActionView::TestCase

# When testing view helpers we don't actually have access to request. So
# here's a fake one.
class FakeRequest

attr_accessor :host_with_port, :fullpath

def initialize
@host_with_port = "www.example.com"
@fullpath = "/"
end

end

setup do
@request = FakeRequest.new
end

def request
@request ||= FakeRequest.new
end

# Expected and actual are wrapped in a root tag to ensure proper XML structure.
def assert_xml_equal(expected, actual)
expected_xml = Nokogiri::XML("<test-xml>\n#{expected}\n</test-xml>", &:noblanks)
Expand Down

0 comments on commit 56c63e5

Please sign in to comment.