snippet identifier, not slug
Oleg
committed Dec 03, 2011
commit d02285bb3f4cf056aec7a1e36b475a617e811056
Showing 17
changed files with
74 additions
and 70 deletions
app/models/cms/snippet.rb
+5
-3
| @@ | @@ -22,7 +22,7 @@ class Cms::Snippet < ActiveRecord::Base |
| :presence => true | |
| validates :label, | |
| :presence => true | |
| - | validates :slug, |
| + | validates :identifier, |
| :presence => true, | |
| :uniqueness => { :scope => :site_id }, | |
| :format => { :with => /^\w[a-z0-9_-]*$/i } | |
| @@ | @@ -33,14 +33,16 @@ class Cms::Snippet < ActiveRecord::Base |
| protected | |
| def assign_label | |
| - | self.label = self.label.blank?? self.slug.try(:titleize) : self.label |
| + | self.label = self.label.blank?? self.identifier.try(:titleize) : self.label |
| end | |
| # Note: This might be slow. We have no idea where the snippet is used, so | |
| # gotta reload every single page. Kinda sucks, but might be ok unless there | |
| # are hundreds of pages. | |
| def clear_cached_page_content | |
| - | site.pages.all.each{ |page| page.save } |
| + | site.pages.all.each do |p| |
| + | Cms::Page.where(:id => p.id).update_all(:content => p.content(true)) |
| + | end |
| end | |
| def assign_position | |
app/views/cms_admin/snippets/_form.html.erb
+1
-1
| @@ | @@ -3,7 +3,7 @@ |
| <% end %> | |
| <%= form.text_field :label, :id => (@snippet.new_record?? 'slugify' : nil) %> | |
| - | <%= form.text_field :slug, :id => 'slug', :class => 'delimiter-underscore' %> |
| + | <%= form.text_field :identifier, :id => 'slug', :class => 'delimiter-underscore' %> |
| <%= form.text_area :content, :class => 'code' %> | |
| <%= render :partial => 'cms_admin/categories/form', :object => form %> | |
app/views/cms_admin/snippets/index.html.erb
+1
-1
| @@ | @@ -24,7 +24,7 @@ |
| <%= link_to snippet.label, edit_cms_admin_site_snippet_path(@site, snippet) %> | |
| <%= render :partial => '/cms_admin/categories/categories', :object => snippet %> | |
| <div class='sublabel'> | |
| - | <%= link_to snippet.slug, edit_cms_admin_site_snippet_path(@site, snippet) %> |
| + | <%= link_to snippet.identifier, edit_cms_admin_site_snippet_path(@site, snippet) %> |
| </div> | |
| </div> | |
| </div> | |
migrate/01_create_cms.rb b/db/migrate/01_create_cms.rb
+6
-6
| @@ | @@ -59,15 +59,15 @@ class CreateCms < ActiveRecord::Migration |
| # -- Snippets ----------------------------------------------------------- | |
| create_table :cms_snippets do |t| | |
| - | t.integer :site_id |
| - | t.string :label |
| - | t.string :slug |
| + | t.integer :site_id, :null => false |
| + | t.string :label, :null => false |
| + | t.string :identifier, :null => false |
| t.text :content | |
| - | t.integer :position, :null => false, :default => 0 |
| - | t.boolean :is_shared, :null => false, :default => false |
| + | t.integer :position, :null => false, :default => 0 |
| + | t.boolean :is_shared, :null => false, :default => false |
| t.timestamps | |
| end | |
| - | add_index :cms_snippets, [:site_id, :slug], :unique => true |
| + | add_index :cms_snippets, [:site_id, :identifier], :unique => true |
| add_index :cms_snippets, [:site_id, :position] | |
| # -- Files -------------------------------------------------------------- | |
comfortable_mexican_sofa/extensions/is_mirrored.rb b/lib/comfortable_mexican_sofa/extensions/is_mirrored.rb
+3
-3
| @@ | @@ -24,7 +24,7 @@ module ComfortableMexicanSofa::IsMirrored |
| case self | |
| when Cms::Layout then site.layouts.find_by_identifier(self.identifier) | |
| when Cms::Page then site.pages.find_by_full_path(self.full_path) | |
| - | when Cms::Snippet then site.snippets.find_by_slug(self.slug) |
| + | when Cms::Snippet then site.snippets.find_by_identifier(self.identifier) |
| end | |
| end.compact | |
| end | |
| @@ | @@ -54,9 +54,9 @@ module ComfortableMexicanSofa::IsMirrored |
| } | |
| m | |
| when Cms::Snippet | |
| - | m = site.snippets.find_by_slug(self.slug_was || self.slug) || site.snippets.new |
| + | m = site.snippets.find_by_identifier(self.identifier_was || self.identifier) || site.snippets.new |
| m.attributes = { | |
| - | :slug => self.slug |
| + | :identifier => self.identifier |
| } | |
| m | |
| end | |
comfortable_mexican_sofa/fixtures.rb b/lib/comfortable_mexican_sofa/fixtures.rb
+6
-6
| @@ | @@ -146,11 +146,11 @@ module ComfortableMexicanSofa::Fixtures |
| snippet_ids = [] | |
| Dir.glob("#{path}/*").select{|f| File.directory?(f)}.each do |path| | |
| - | slug = path.split('/').last |
| - | snippet = site.snippets.find_by_slug(slug) || site.snippets.new(:slug => slug) |
| + | identifier = path.split('/').last |
| + | snippet = site.snippets.find_by_identifier(identifier) || site.snippets.new(:identifier => identifier) |
| # updating attributes | |
| - | if File.exists?(file_path = File.join(path, "_#{slug}.yml")) |
| + | if File.exists?(file_path = File.join(path, "_#{identifier}.yml")) |
| if snippet.new_record? || File.mtime(file_path) > snippet.updated_at | |
| attributes = YAML.load_file(file_path).symbolize_keys! | |
| snippet.label = attributes[:label] || slug.titleize | |
| @@ | @@ -169,7 +169,7 @@ module ComfortableMexicanSofa::Fixtures |
| # saving | |
| if snippet.changed? | |
| snippet.save! | |
| - | Rails.logger.debug "[Fixtures] Saved Snippet {#{snippet.slug}}" |
| + | Rails.logger.debug "[Fixtures] Saved Snippet {#{snippet.identifier}}" |
| end | |
| snippet_ids << snippet.id | |
| end | |
| @@ | @@ -243,8 +243,8 @@ module ComfortableMexicanSofa::Fixtures |
| FileUtils.mkdir_p(path) | |
| site.snippets.each do |snippet| | |
| - | FileUtils.mkdir_p(snippet_path = File.join(path, snippet.slug)) |
| - | open(File.join(snippet_path, "_#{snippet.slug}.yml"), 'w') do |f| |
| + | FileUtils.mkdir_p(snippet_path = File.join(path, snippet.identifier)) |
| + | open(File.join(snippet_path, "_#{snippet.identifier}.yml"), 'w') do |f| |
| f.write({'label' => snippet.label}.to_yaml) | |
| end | |
| open(File.join(snippet_path, 'content.html'), 'w') do |f| | |
comfortable_mexican_sofa/tags/snippet.rb b/lib/comfortable_mexican_sofa/tags/snippet.rb
+1
-1
| @@ | @@ -8,7 +8,7 @@ class ComfortableMexicanSofa::Tag::Snippet |
| # Find or initialize Cms::Snippet object | |
| def snippet | |
| - | page.site.snippets.detect{|s| s.slug == self.label.to_s} || page.site.snippets.build(:slug => self.label.to_s) |
| + | page.site.snippets.detect{|s| s.identifier == self.label.to_s} || page.site.snippets.build(:identifier => self.label.to_s) |
| end | |
| def content | |
comfortable_mexican_sofa/view_methods.rb b/lib/comfortable_mexican_sofa/view_methods.rb
+4
-4
| @@ | @@ -17,13 +17,13 @@ module ComfortableMexicanSofa::ViewMethods |
| # Content of a snippet. Example: | |
| # cms_snippet_content(:my_snippet) | |
| - | def cms_snippet_content(snippet_slug, cms_site = nil) |
| + | def cms_snippet_content(identifier, cms_site = nil) |
| return '' unless cms_site ||= (@cms_site || Cms::Site.find_site(request.host.downcase, request.fullpath)) | |
| - | case snippet_slug |
| + | case identifier |
| when Cms::Snippet | |
| - | snippet = snippet_slug |
| + | snippet = identifier |
| else | |
| - | return '' unless snippet = cms_site.snippets.find_by_slug(snippet_slug) |
| + | return '' unless snippet = cms_site.snippets.find_by_identifier(identifier) |
| end | |
| render :inline => ComfortableMexicanSofa::Tag.process_content(cms_site.pages.build, snippet.content) | |
| end | |
test/fixtures/cms/snippets.yml
+1
-1
| @@ | @@ -1,6 +1,6 @@ |
| default: | |
| site: default | |
| label: Default Snippet | |
| - | slug: default |
| + | identifier: default |
| content: default_snippet_content | |
| position: 0 | |
test/functional/cms_admin/snippets_controller_test.rb
+7
-7
| @@ | @@ -63,9 +63,9 @@ class CmsAdmin::SnippetsControllerTest < ActionController::TestCase |
| def test_create | |
| assert_difference 'Cms::Snippet.count' do | |
| post :create, :site_id => cms_sites(:default), :snippet => { | |
| - | :label => 'Test Snippet', |
| - | :slug => 'test-snippet', |
| - | :content => 'Test Content' |
| + | :label => 'Test Snippet', |
| + | :identifier => 'test-snippet', |
| + | :content => 'Test Content' |
| } | |
| assert_response :redirect | |
| snippet = Cms::Snippet.last | |
| @@ | @@ -101,12 +101,12 @@ class CmsAdmin::SnippetsControllerTest < ActionController::TestCase |
| def test_update_failure | |
| snippet = cms_snippets(:default) | |
| put :update, :site_id => snippet.site, :id => snippet, :snippet => { | |
| - | :slug => '' |
| + | :identifier => '' |
| } | |
| assert_response :success | |
| assert_template :edit | |
| snippet.reload | |
| - | assert_not_equal '', snippet.slug |
| + | assert_not_equal '', snippet.identifier |
| assert_equal 'Failed to update snippet', flash[:error] | |
| end | |
| @@ | @@ -122,8 +122,8 @@ class CmsAdmin::SnippetsControllerTest < ActionController::TestCase |
| def test_reorder | |
| snippet_one = cms_snippets(:default) | |
| snippet_two = cms_sites(:default).snippets.create!( | |
| - | :label => 'test', |
| - | :slug => 'test' |
| + | :label => 'test', |
| + | :identifier => 'test' |
| ) | |
| assert_equal 0, snippet_one.position | |
| assert_equal 1, snippet_two.position | |
test/integration/fixtures_test.rb
+2
-2
| @@ | @@ -14,7 +14,7 @@ class FixturesTest < ActionDispatch::IntegrationTest |
| assert_equal 'Default Page', Cms::Page.root.label | |
| assert_equal 'Default Layout', Cms::Layout.find_by_identifier('default').label | |
| - | assert_equal 'Default Snippet', Cms::Snippet.find_by_slug('default').label |
| + | assert_equal 'Default Snippet', Cms::Snippet.find_by_identifier('default').label |
| end | |
| end | |
| @@ | @@ -32,7 +32,7 @@ class FixturesTest < ActionDispatch::IntegrationTest |
| assert_equal 'Home Fixture Page', Cms::Page.root.label | |
| assert_equal 'Default Fixture Layout', Cms::Layout.find_by_identifier('default').label | |
| - | assert_equal 'Default Fixture Snippet', Cms::Snippet.find_by_slug('default').label |
| + | assert_equal 'Default Fixture Snippet', Cms::Snippet.find_by_identifier('default').label |
| assert_equal "<html>\n <body>\n Home Page Fixture Content\nFixture Content for Default Snippet\n </body>\n</html>", response.body | |
| end | |
test/unit/fixtures_test.rb
+7
-7
| @@ | @@ -139,7 +139,7 @@ class FixturesTest < ActiveSupport::TestCase |
| assert_difference 'Cms::Snippet.count' do | |
| ComfortableMexicanSofa::Fixtures.import_snippets('test.host', 'example.com') | |
| assert snippet = Cms::Snippet.last | |
| - | assert_equal 'default', snippet.slug |
| + | assert_equal 'default', snippet.identifier |
| assert_equal 'Default Fixture Snippet', snippet.label | |
| assert_equal 'Fixture Content for Default Snippet', snippet.content | |
| end | |
| @@ | @@ -148,14 +148,14 @@ class FixturesTest < ActiveSupport::TestCase |
| def test_import_snippets_updating | |
| snippet = cms_snippets(:default) | |
| snippet.update_attribute(:updated_at, 10.years.ago) | |
| - | assert_equal 'default', snippet.slug |
| + | assert_equal 'default', snippet.identifier |
| assert_equal 'Default Snippet', snippet.label | |
| assert_equal 'default_snippet_content', snippet.content | |
| assert_no_difference 'Cms::Snippet.count' do | |
| ComfortableMexicanSofa::Fixtures.import_snippets('test.host', 'example.com') | |
| snippet.reload | |
| - | assert_equal 'default', snippet.slug |
| + | assert_equal 'default', snippet.identifier |
| assert_equal 'Default Fixture Snippet', snippet.label | |
| assert_equal 'Fixture Content for Default Snippet', snippet.content | |
| end | |
| @@ | @@ -163,16 +163,16 @@ class FixturesTest < ActiveSupport::TestCase |
| def test_import_snippets_deleting | |
| snippet = cms_snippets(:default) | |
| - | snippet.update_attribute(:slug, 'old') |
| + | snippet.update_attribute(:identifier, 'old') |
| assert_no_difference 'Cms::Snippet.count' do | |
| ComfortableMexicanSofa::Fixtures.import_snippets('test.host', 'example.com') | |
| assert snippet = Cms::Snippet.last | |
| - | assert_equal 'default', snippet.slug |
| + | assert_equal 'default', snippet.identifier |
| assert_equal 'Default Fixture Snippet', snippet.label | |
| assert_equal 'Fixture Content for Default Snippet', snippet.content | |
| - | assert_nil Cms::Snippet.find_by_slug('old') |
| + | assert_nil Cms::Snippet.find_by_identifier('old') |
| end | |
| end | |
| @@ | @@ -187,7 +187,7 @@ class FixturesTest < ActiveSupport::TestCase |
| ComfortableMexicanSofa::Fixtures.import_snippets('test.host', 'example.com') | |
| snippet.reload | |
| - | assert_equal 'default', snippet.slug |
| + | assert_equal 'default', snippet.identifier |
| assert_equal 'Default Snippet', snippet.label | |
| assert_equal 'default_snippet_content', snippet.content | |
| end | |
test/unit/mirrors_test.rb
+7
-7
| @@ | @@ -31,9 +31,9 @@ class MirrorsTest < ActiveSupport::TestCase |
| def test_snippet_creation | |
| assert_difference 'Cms::Snippet.count', 2 do | |
| - | snippet = @site_a.snippets.create(:slug => 'test') |
| + | snippet = @site_a.snippets.create(:identifier => 'test') |
| assert_equal 1, snippet.mirrors.size | |
| - | assert_equal 'test', snippet.mirrors.first.slug |
| + | assert_equal 'test', snippet.mirrors.first.identifier |
| end | |
| end | |
| @@ | @@ -84,14 +84,14 @@ class MirrorsTest < ActiveSupport::TestCase |
| end | |
| def test_snippet_update | |
| - | snippet_1 = @site_a.snippets.create(:slug => 'test') |
| + | snippet_1 = @site_a.snippets.create(:identifier => 'test') |
| assert snippet_2 = snippet_1.mirrors.first | |
| snippet_1.update_attributes!( | |
| - | :slug => 'updated', |
| - | :content => 'updated content' |
| + | :identifier => 'updated', |
| + | :content => 'updated content' |
| ) | |
| snippet_2.reload | |
| - | assert_equal 'updated', snippet_2.slug |
| + | assert_equal 'updated', snippet_2.identifier |
| assert_not_equal 'updated content', snippet_2.content | |
| end | |
| @@ | @@ -137,7 +137,7 @@ class MirrorsTest < ActiveSupport::TestCase |
| end | |
| def test_snippet_destroy | |
| - | snippet_1 = @site_a.snippets.create(:slug => 'test') |
| + | snippet_1 = @site_a.snippets.create(:identifier => 'test') |
| assert snippet_2 = snippet_1.mirrors.first | |
| assert_difference ['@site_a.snippets.count', '@site_b.snippets.count'], -1 do | |
test/unit/models/snippet_test.rb
+6
-6
| @@ | @@ -12,12 +12,12 @@ class CmsSnippetTest < ActiveSupport::TestCase |
| snippet = Cms::Snippet.new | |
| snippet.save | |
| assert snippet.invalid? | |
| - | assert_has_errors_on snippet, [:label, :slug] |
| + | assert_has_errors_on snippet, [:label, :identifier] |
| end | |
| def test_label_assignment | |
| snippet = cms_sites(:default).snippets.new( | |
| - | :slug => 'test' |
| + | :identifier => 'test' |
| ) | |
| assert snippet.valid? | |
| assert_equal 'Test', snippet.label | |
| @@ | @@ -26,12 +26,12 @@ class CmsSnippetTest < ActiveSupport::TestCase |
| def test_create | |
| assert_difference 'Cms::Snippet.count' do | |
| snippet = cms_sites(:default).snippets.create( | |
| - | :label => 'Test Snippet', |
| - | :slug => 'test', |
| - | :content => 'Test Content' |
| + | :label => 'Test Snippet', |
| + | :identifier => 'test', |
| + | :content => 'Test Content' |
| ) | |
| assert_equal 'Test Snippet', snippet.label | |
| - | assert_equal 'test', snippet.slug |
| + | assert_equal 'test', snippet.identifier |
| assert_equal 'Test Content', snippet.content | |
| assert_equal 1, snippet.position | |
| end | |
test/unit/revisions_test.rb
+3
-3
| @@ | @@ -107,9 +107,9 @@ class RevisionsTest < ActiveSupport::TestCase |
| assert_difference 'Cms::Snippet.count' do | |
| assert_no_difference 'Cms::Revision.count' do | |
| snippet = cms_sites(:default).snippets.create!( | |
| - | :label => 'test snippet', |
| - | :slug => 'test_snippet', |
| - | :content => 'test content' |
| + | :label => 'test snippet', |
| + | :identifier => 'test_snippet', |
| + | :content => 'test content' |
| ) | |
| assert_equal 0, snippet.revisions.count | |
| end | |
test/unit/tag_test.rb
+4
-4
| @@ | @@ -229,8 +229,8 @@ class TagTest < ActiveSupport::TestCase |
| :content => '<% 1 + 1 %> {{cms:page:content}} {{cms:collection:snippet:cms/snippet}} <%= 1 + 1 %>' | |
| ) | |
| snippet = site.snippets.create!( | |
| - | :slug => 'no-irb-snippet', |
| - | :content => '<% 2 + 2 %> snippet <%= 2 + 2 %>' |
| + | :identifier => 'no-irb-snippet', |
| + | :content => '<% 2 + 2 %> snippet <%= 2 + 2 %>' |
| ) | |
| page = site.pages.create!( | |
| :slug => 'no-irb-page', | |
| @@ | @@ -255,8 +255,8 @@ class TagTest < ActiveSupport::TestCase |
| :content => '<% 1 + 1 %> {{cms:page:content}} {{cms:collection:snippet:cms/snippet}} <%= 1 + 1 %>' | |
| ) | |
| snippet = site.snippets.create!( | |
| - | :slug => 'irb-snippet', |
| - | :content => '<% 2 + 2 %> snippet <%= 2 + 2 %>' |
| + | :identifier => 'irb-snippet', |
| + | :content => '<% 2 + 2 %> snippet <%= 2 + 2 %>' |
| ) | |
| page = site.pages.create!( | |
| :slug => 'irb-page', | |
test/unit/tags/collection_test.rb
+10
-8
| @@ | @@ -4,7 +4,7 @@ class CollectionTagTest < ActiveSupport::TestCase |
| module TestCollectionScope | |
| def self.included(base) | |
| - | base.scope :cms_collection, lambda{|*args| base.where(:slug => args.first) if args.first } |
| + | base.scope :cms_collection, lambda{|*args| base.where(:identifier => args.first) if args.first } |
| end | |
| end | |
| Cms::Snippet.send(:include, TestCollectionScope) | |
| @@ | @@ -23,13 +23,14 @@ class CollectionTagTest < ActiveSupport::TestCase |
| def test_initialize_tag_detailed | |
| assert tag = ComfortableMexicanSofa::Tag::Collection.initialize_tag( | |
| - | cms_pages(:default), '{{ cms:collection:snippet:cms/snippet:path/to/partial:title:slug:param_a:param_b }}' |
| + | cms_pages(:default), |
| + | '{{ cms:collection:snippet:cms/snippet:path/to/partial:title:identifier:param_a:param_b }}' |
| ) | |
| assert_equal 'snippet', tag.label | |
| assert_equal 'Cms::Snippet', tag.collection_class | |
| assert_equal 'path/to/partial', tag.collection_partial | |
| assert_equal 'title', tag.collection_title | |
| - | assert_equal 'slug', tag.collection_identifier |
| + | assert_equal 'identifier', tag.collection_identifier |
| assert_equal ['param_a', 'param_b'], tag.collection_params | |
| end | |
| @@ | @@ -57,7 +58,8 @@ class CollectionTagTest < ActiveSupport::TestCase |
| def test_collection_objects_with_scope | |
| assert tag = ComfortableMexicanSofa::Tag::Collection.initialize_tag( | |
| - | cms_pages(:default), "{{ cms:collection:snippet:cms/snippet:path/to/partial:label:slug:#{cms_snippets(:default).slug} }}" |
| + | cms_pages(:default), |
| + | "{{ cms:collection:snippet:cms/snippet:path/to/partial:label:identifier:#{cms_snippets(:default).identifier} }}" |
| ) | |
| assert snippets = tag.collection_objects | |
| assert_equal 1, snippets.count | |
| @@ | @@ -90,10 +92,10 @@ class CollectionTagTest < ActiveSupport::TestCase |
| assert tag.block.content.blank? | |
| snippet = cms_snippets(:default) | |
| - | tag.block.content = snippet.slug |
| - | assert_equal snippet.slug, tag.block.content |
| - | assert_equal snippet.slug, tag.content |
| - | assert_equal "<%= render :partial => 'path/to/partial', :locals => {:model => 'Cms::Snippet', :identifier => '#{snippet.slug}', :param_1 => 'param_a', :param_2 => 'param_b'} %>", tag.render |
| + | tag.block.content = snippet.identifier |
| + | assert_equal snippet.identifier, tag.block.content |
| + | assert_equal snippet.identifier, tag.content |
| + | assert_equal "<%= render :partial => 'path/to/partial', :locals => {:model => 'Cms::Snippet', :identifier => '#{snippet.identifier}', :param_1 => 'param_a', :param_2 => 'param_b'} %>", tag.render |
| end | |
| def test_content_and_render_with_no_content | |