Skip to content

Instantly share code, notes, and snippets.

@jhenry
Created June 22, 2011 03:11
Show Gist options
  • Save jhenry/1039434 to your computer and use it in GitHub Desktop.
Save jhenry/1039434 to your computer and use it in GitHub Desktop.
module CmsHelper
def embed_calendar_widget(area_slug)
calendar = Cms::Page.find_by_slug("#{area_slug}_calendar")
is_enabled = get_page_block(calendar.blocks, "is_enabled")
widget_body = get_page_block(calendar.blocks, "widget_body")
if is_enabled == 'true'
content_tag(:div, widget_body, :id => "calendar_widget")
end
end
private
def get_page_block(blocks, label)
blocks.select { |b| b["label"] == label}[0].content
end
end
require 'test_helper'
class CmsHelperTest < ActionView::TestCase
fixtures :all
def setup
build_calendar_widget_layout
build_calendar_pages
end
def test_embed_calendar_widget
render :text => embed_calendar_widget("huntington")
assert_select "div#calendar_widget", "huntington embedded calendar code"
end
def test_calendar_does_not_embed_when_not_enabled
render :text => embed_calendar_widget("fivesisters")
assert_select "div#calendar_widget", false, "fivesisters embedded calendar code"
end
private
def build_calendar_widget_layout
@calendar_widget_layout = cms_sites(:default).layouts.create(
:label => 'Calendar Widget',
:slug => 'calendar_widget',
:content => '{{cms:page:widget_body:text}} {{cms:page:is_calendar:string}} {{cms:page:is_enabled:string}} {{cms:page:area_slug:string}}',
:css => 'css',
:js => 'js'
)
end
def build_calendar_pages
@calendar_widget = cms_sites(:default).pages.create!(
:slug => "huntington_calendar",
:label => "Huntington Calendar Widget",
:parent => cms_pages(:default),
:layout => @calendar_widget_layout,
:blocks_attributes => [
{
:label => 'widget_body',
:content => 'huntington embedded calendar code'
},
{
:label => 'area_slug',
:content => 'huntington'
},
{
:label => 'is_enabled',
:content => 'true'
},
{
:label => 'is_calendar',
:content => 'true'
}
]
)
@calendar_widget = cms_sites(:default).pages.create!(
:slug => "fivesisters_calendar",
:label => "Five Sisters Calendar Widget",
:parent => cms_pages(:default),
:layout => @calendar_widget_layout,
:blocks_attributes => [
{
:label => 'widget_body',
:content => 'five sisters embedded calendar code'
},
{
:label => 'area_slug',
:content => 'fivesisters'
},
{
:label => 'is_enabled',
:content => 'false'
},
{
:label => 'is_calendar',
:content => 'true'
}
]
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment