diff --git a/README.md b/README.md index c2a48f9..add7242 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,9 @@ About An extension by [Aissac][aissac] that adds templating support to [Radiant CMS][radiant]. -Tested on Radiant 0.7.1, 0.8 and 0.9 RC2. +Tested on Radiant 0.7.1, 0.8 and 0.9.1. + +Checkout the [screencast][rc]! Features --- @@ -50,23 +52,23 @@ Finally, install the [Stereotype Extension][rste] Configuration --- -To add stereotypes you need to add fields to the Radiant::Config table. There are three types of settings for each stereotype: +To add stereotypes you need to add fields to the Radiant::Config table. There are five types of settings for each stereotype: 1.) Page Parts (and filters) -The key has to look like: `stereotype..parts` and the value `body:markdown,sidebar:textile`. +The key has to look like: `stereotype..parts` and the value `body:Markdown,sidebar:Textile` (case sensitive). 2.) Layout -The key has to look like: `stereotype..layout` and the value has to be the name of a layout (case sensitive) +The key has to look like: `stereotype..layout` and the value has to be the name of a layout (case sensitive). 3.) Page type -The key should look like: `stereotype..page_type` and the value has to be a valid `class_name` attribute of the Page class. (Examples: ArchivePage, FileNotFoundPage or ArchiveMonthIndexPage) +The key should look like: `stereotype..page_type` and the value has to be a valid `class_name` attribute of the Page class. (Examples: ArchivePage, FileNotFoundPage or ArchiveMonthIndexPage). 4.) Status -The key has to look like: `stereotype..status` and the value has to be a valid `status` attribute of the page: draft, hidden, reviewed, published +The key has to look like: `stereotype..status` and the value has to be a valid `status` attribute of the page: draft, hidden, reviewed, published. 5.) Stereotype @@ -83,16 +85,32 @@ Usage 4.) Profit! +I18n +--- + +This extension is translated to English and Romanian. + +If you happen to translate it to some other languages please send a pull request. + +Patches +--- + +If you want to contribute features or fixes please write your specs/cucumber features and code and submit pull requests to these github users: + + * cristi + * ihoka + Contributors --- -* Cristi Duma ([@cristi_duma][cd]) +* Cristi Duma ([@cristiduma][cd]) * Istvan Hoka ([@ihoka][ih]) [radiant]: http://radiantcms.org/ -[aissac]: http://aissac.ro +[aissac]: http://aissac.ro/en [rste]: http://blog.aissac.ro/radiant/stereotype-extension [rcfe]: http://blog.aissac.ro/radiant/custom-fields-extension [rse]: http://github.com/Squeegy/radiant-settings/tree/master -[cd]: http://twitter.com/cristi_duma -[ih]: http://twitter.com/ihoka \ No newline at end of file +[cd]: http://twitter.com/cristiduma +[ih]: http://twitter.com/ihoka +[rc]: http://radiantcms.org/blog/archives/2010/03/23/radiantcasts-episode-6-radiant-custom-fields-and-stereotype-extensions/ \ No newline at end of file diff --git a/lib/stereotype/page_extensions.rb b/lib/stereotype/page_extensions.rb index 8d80d12..a3d4178 100644 --- a/lib/stereotype/page_extensions.rb +++ b/lib/stereotype/page_extensions.rb @@ -31,41 +31,66 @@ def update_custom_fields end module ClassMethods - def new_with_defaults_with_stereotype(config = Radiant::Config) + def new_with_defaults_with_stereotype(config=Radiant::Config) page = new custom_field = CustomField.find(:first, :conditions => { :name => "stereotype", :page_id => page.parent_id}) name = custom_field && custom_field.value if name - parts_and_filters = config["stereotype.#{name}.parts"].blank? ? config["defaults.page.parts"].to_s.strip.split(',') : config["stereotype.#{name}.parts"].to_s.strip.split(',') + set_page_parts(page, name, config) - parts_and_filters.each do |part_and_filter| - part_filter = part_and_filter.to_s.split(':') - - st_name = part_filter[0].nil? ? "" : part_filter[0] - st_filter = part_filter[1].nil? ? "" : part_filter[1] - page.parts << PagePart.new(:name => st_name, :filter_id => st_filter) - end - - st_layout = Layout.find_by_name(config["stereotype.#{name}.layout"]) - st_layout_id = st_layout.nil? ? nil : st_layout.id - page.layout_id = st_layout_id if st_layout_id - - st_class_name = config["stereotype.#{name}.page_type"] - page.class_name = st_class_name if st_class_name + set_page_status(page, name, config) + + set_page_layout(page, name, config) - st_status = config["stereotype.#{name}.status"] - page.status = Status[st_status] if st_status + set_page_class_name(page, name, config) - st_stereotype = config["stereotype.#{name}.stereotype"] - page.stereotype = st_stereotype if st_stereotype + set_page_stereotype(page, name, config) page else new_with_defaults_without_stereotype(config) end end + private + def set_page_parts(page, name, config) + if config["stereotype.#{name}.parts"].blank? + page.parts << default_page_parts(config) + else + page.parts << stereotype_page_parts(name, config) + end + + end + + def stereotype_page_parts(name, config) + parts_and_filters = config["stereotype.#{name}.parts"].to_s.strip.split(',') + + parts_and_filters.map do |part_and_filter| + part_name, filter = part_and_filter.to_s.split(':') + PagePart.new(:name => part_name, :filter_id => filter) + end + end + + def set_page_status(page, name, config) + default_status = config["stereotype.#{name}.status"] || config['defaults.page.status'] + page.status = Status[default_status] if default_status + end + + def set_page_layout(page, name, config) + default_layout = Layout.find_by_name(config["stereotype.#{name}.layout"]) + page.layout_id = default_layout.id if default_layout + end + + def set_page_class_name(page, name, config) + default_page_type = config["stereotype.#{name}.page_type"] + page.class_name = default_page_type if default_page_type + end + + def set_page_stereotype(page, name, config) + default_stereotype = config["stereotype.#{name}.stereotype"] + page.stereotype = default_stereotype if default_stereotype + end end end end \ No newline at end of file diff --git a/spec/controllers/pages_controller_spec.rb b/spec/controllers/pages_controller_spec.rb index b42aea5..847ac29 100644 --- a/spec/controllers/pages_controller_spec.rb +++ b/spec/controllers/pages_controller_spec.rb @@ -13,9 +13,7 @@ configuration = { 'stereotype.post.layout' => 'Normal', 'stereotype.post.parts' => 'body:Textile,sidebar:Markdown', - 'stereotype.post.page_type' => '', - 'stereotype.post.status' => 'published', - 'stereotype.post.stereotype' => '' + 'stereotype.post.status' => 'published' } configuration.each {|k,v| Radiant::Config.create!(:key => k, :value => v) } diff --git a/spec/models/page_spec.rb b/spec/models/page_spec.rb index a67288f..40c7286 100644 --- a/spec/models/page_spec.rb +++ b/spec/models/page_spec.rb @@ -31,43 +31,90 @@ end describe Page, "#new_with_defaults_with_stereotype" do - before do - @parent = pages(:parent) - @page = @parent.children.new_with_defaults_with_stereotype(config_hash) - end - - it "creates a new page with stereotype.post.layout Layout" do - layout = layouts(:main) - @page.layout_id.should == layout.id - end + subject { pages(:parent).children.new_with_defaults_with_stereotype(config) } - it "creates a new page with stereotype.post.page_type ClassName" do - @page.class_name.should == "ArchivePage" + context "specifying page parts and filters" do + context "when page parts and filters are specified in stereotype" do + let(:config) { + { "stereotype.post.parts" => "body:Textile,sidebar:Markdown" } + } + + it "creates a new page with specified parts and filters" do + subject.parts[0].name.should == "body" + subject.parts[1].name.should == "sidebar" + subject.parts[0].filter_id.should == "Textile" + subject.parts[1].filter_id.should == "Markdown" + end + end + + context "when page parts and filters are not specified in stereotype" do + let(:config) { + { + "defaults.page.parts" => "body,extended", + "defaults.page.filter" => "Textile" + } + } + + it "creates a new page with default parts and filters" do + subject.parts[0].name.should == "body" + subject.parts[1].name.should == "extended" + subject.parts[0].filter_id.should == "Textile" + subject.parts[1].filter_id.should == "Textile" + end + end end - it "creates a new page with stereotype.post.parts Page Parts and Filters" do - @page.parts[0].name.should == "body" - @page.parts[1].name.should == "sidebar" - @page.parts[0].filter_id.should == "Textile" - @page.parts[1].filter_id.should == "Markdown" + context "specifying status" do + context "when status specified by stereotype" do + let(:config) { + { "stereotype.post.status" => "Published" } + } + + it "creates a new page with specified status" do + subject.status_id.should == Status["published"].id + end + end + + context "when status not specified by stereotype" do + let(:config) { + { "defaults.page.status" => "draft" } + } + + it "creates a new page with default " do + subject.status_id.should == Status["draft"].id + end + end end - it "creates a new page with stereotype.post.status Status" do - @page.status_id.should == Status["published"].id + context "specifying layout" do + let(:config) { + { "stereotype.post.layout" => "Main" } + } + + it "creates a new page with stereotype specified layout" do + layout = layouts(:main) + subject.layout_id.should == layout.id + end + end + + context "specifying page type" do + let(:config) { + { "stereotype.post.page_type" => "ArchivePage" } + } + + it "creates a new page with stereotype specified class name" do + subject.class_name.should == "ArchivePage" + end end - it "creates a new page with stereotype.post.stereotype Stereotype" do - @page.stereotype.should == "post_child" + context "specified stereotype" do + let(:config) { + { "stereotype.post.stereotype" => "post_child" } + } + + it "creates a new page with stereotype specified stereotype" do + subject.stereotype.should == "post_child" + end end end - - def config_hash - { - "stereotype.post.page_type" => "ArchivePage", - "stereotype.post.layout" => "Main", - "stereotype.post.parts" => "body:Textile,sidebar:Markdown", - "stereotype.post.status" => "published", - "stereotype.post.stereotype" => "post_child" - } - end end \ No newline at end of file diff --git a/stereotype_extension.rb b/stereotype_extension.rb index 0c2c611..6aca6df 100644 --- a/stereotype_extension.rb +++ b/stereotype_extension.rb @@ -1,7 +1,7 @@ require_dependency 'application_controller' class StereotypeExtension < Radiant::Extension - version "0.9" # this version is compatible with Radiant 0.9RC2 + version "0.9" # this version is compatible with Radiant 0.9 description "Define templates for Pages' children in Radiant CMS." url "http://blog.aissac.ro/radiant/stereotype-extension/"