snippets have labels and slugs now. please and thank you

Oleg committed Oct 19, 2010
commit 504ad4c6ccc980f445f167bb64c12e125a0e5133
Showing 10 changed files with 31 additions and 12 deletions
app/models/cms_snippet.rb +6 -4
@@ @@ -7,17 +7,19 @@ class CmsSnippet < ActiveRecord::Base
validates :cms_site_id,
:presence => true
validates :label,
+ :presence => true
+ validates :slug,
:presence => true,
:uniqueness => { :scope => :cms_site_id },
:format => { :with => /^\w[a-z0-9_-]*$/i }
# -- Class Methods --------------------------------------------------------
- def self.content_for(label)
- (s = find_by_label(label)) ? s.content : ''
+ def self.content_for(slug)
+ (s = find_by_slug(slug)) ? s.content : ''
end
- def self.initialize_or_find(cms_page, label)
- find_by_label(label) || new(:label => label)
+ def self.initialize_or_find(cms_page, slug)
+ find_by_slug(slug) || new(:slug => slug)
end
end
app/views/cms_admin/snippets/_form.html.erb +2 -1
@@ @@ -1,2 +1,3 @@
- <%= form.text_field :label %>
+ <%= form.text_field :label, :id => (@cms_snippet.new_record?? 'slugify' : nil) %>
+ <%= form.text_field :slug, :id => 'slug' %>
<%= form.text_area :content %>
\ No newline at end of file
app/views/cms_admin/snippets/index.html.erb +10 -1
@@ @@ -1,3 +1,12 @@
+ <%= link_to span_tag('Create New Snippet'), new_cms_admin_snippet_path, :class => 'big_button' %>
<h1>Snippets</h1>
- <%= debug @cms_snippets %>
\ No newline at end of file
+ <table class='list'>
+ <% @cms_snippets.each do |snippet| %>
+ <tr>
+ <td class='main'>
+ <%= link_to snippet.label, edit_cms_admin_snippet_path(snippet)%>
+ </td>
+ </tr>
+ <% end %>
+ </table>
\ No newline at end of file
migrate/01_create_cms.rb b/db/migrate/01_create_cms.rb +2 -1
@@ @@ -56,10 +56,11 @@ class CreateCms < ActiveRecord::Migration
create_table :cms_snippets do |t|
t.integer :cms_site_id
t.string :label
+ t.string :slug
t.text :content
t.timestamps
end
- add_index :cms_snippets, [:cms_site_id, :label], :unique => true
+ add_index :cms_snippets, [:cms_site_id, :slug], :unique => true
# -- Assets -------------------------------------------------------------
create_table :cms_uploads do |t|
comfortable_mexican_sofa/cms_tag/snippet.rb b/lib/comfortable_mexican_sofa/cms_tag/snippet.rb +4 -0
@@ @@ -2,6 +2,10 @@ class CmsTag::Snippet < CmsSnippet
include CmsTag
+ def identifier
+ "#{self.class.name.underscore}_#{self.slug}"
+ end
+
def self.regex_tag_signature(label = nil)
label ||= /\w+/
/<\s*cms:snippet:(#{label})\s*\/?>/
comfortable_mexican_sofa/controller_methods.rb b/lib/comfortable_mexican_sofa/controller_methods.rb +1 -1
@@ @@ -18,7 +18,7 @@ module ComfortableMexicanSofa::ControllerMethods
# Now you can render cms_page simply by calling:
# render :cms_page => '/path/to/page'
# This way application controllers can use CMS content while populating
- # instance varaibles that can be used in partials (that are included by
+ # instance variables that can be used in partials (that are included by
# by the cms page and/or layout)
def render_with_cms(options = {}, locals = {}, &block)
if path = options.delete(:cms_page)
test/fixtures/cms_snippets.yml +2 -1
@@ @@ -1,4 +1,5 @@
default:
cms_site: default
- label: default
+ label: Default Snippet
+ slug: default
content: default_snippet_content
test/functional/cms_admin/snippets_controller_test.rb +2 -1
@@ @@ -36,7 +36,8 @@ class CmsAdmin::SnippetsControllerTest < ActionController::TestCase
def test_creation
assert_difference 'CmsSnippet.count' do
post :create, :cms_snippet => {
- :label => 'Test-Snippet',
+ :label => 'Test Snippet',
+ :slug => 'test-snippet',
:content => 'Test Content'
}
assert_response :redirect
test/unit/cms_snippet_test.rb +1 -1
@@ @@ -12,7 +12,7 @@ class CmsSnippetTest < ActiveSupport::TestCase
snippet = CmsSnippet.new
snippet.save
assert snippet.invalid?
- assert_has_errors_on snippet, :label
+ assert_has_errors_on snippet, [:label, :slug]
end
def test_method_content
test/unit/cms_tags/snippet_test.rb +1 -1
@@ @@ -8,7 +8,7 @@ class SnippetTest < ActiveSupport::TestCase
<cms:snippet:label>
).each do |tag_signature|
assert tag = CmsTag::Snippet.initialize_tag(cms_pages(:default), tag_signature)
- assert_equal 'label', tag.label
+ assert_equal 'label', tag.slug
end
end