refinerycms-sl-snippets 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/app/assets/javascripts/page-snippet-picker.js +121 -0
  2. data/app/assets/javascripts/part-snippets-select.js +40 -0
  3. data/app/assets/stylesheets/page-snippet-picker.css +82 -0
  4. data/app/controllers/refinery/admin/snippets_controller.rb +38 -0
  5. data/app/controllers/refinery/admin/snippets_page_parts_controller.rb +38 -0
  6. data/app/models/refinery/snippet.rb +62 -0
  7. data/app/models/refinery/snippet_page_part.rb +20 -0
  8. data/app/views/admin/pages/tabs/_snippets_field.html.erb +46 -0
  9. data/app/views/refinery/admin/pages/tabs/_snippets.html.erb +3 -0
  10. data/app/views/refinery/admin/pages/tabs/_snippets_content.html.erb +24 -0
  11. data/app/views/refinery/admin/pages/tabs/_snippets_field.html.erb +55 -0
  12. data/app/views/refinery/admin/pages/tabs/_snippets_list_item.html.erb +20 -0
  13. data/app/views/refinery/admin/snippets/_actions.html.erb +28 -0
  14. data/app/views/refinery/admin/snippets/_form.html.erb +35 -0
  15. data/app/views/refinery/admin/snippets/_locale_picker.html.erb +12 -0
  16. data/app/views/refinery/admin/snippets/_records.html.erb +18 -0
  17. data/app/views/refinery/admin/snippets/_snippet.html.erb +22 -0
  18. data/app/views/refinery/admin/snippets/_snippets.html.erb +2 -0
  19. data/app/views/refinery/admin/snippets/_sortable_list.html.erb +7 -0
  20. data/app/views/refinery/admin/snippets/edit.html.erb +1 -0
  21. data/app/views/refinery/admin/snippets/index.html.erb +10 -0
  22. data/app/views/refinery/admin/snippets/new.html.erb +1 -0
  23. data/app/views/refinery/admin/snippets_page_parts/add.html.erb +1 -0
  24. data/app/views/refinery/admin/snippets_page_parts/remove.html.erb +1 -0
  25. data/config/locales/cs.yml +35 -0
  26. data/config/locales/en.yml +44 -0
  27. data/config/locales/es.yml +44 -0
  28. data/config/locales/nl.yml +22 -0
  29. data/config/routes.rb +18 -0
  30. data/db/migrate/1_create_snippets.rb +22 -0
  31. data/db/migrate/2_translate_snippets.rb +20 -0
  32. data/db/migrate/3_create_snippets_page_parts.rb +22 -0
  33. data/db/migrate/6_remove_position_from_snippets.rb +12 -0
  34. data/db/seeds.rb +6 -0
  35. data/features/manage_snippets.feature +63 -0
  36. data/features/step_definitions/snippet_steps.rb +14 -0
  37. data/features/support/paths.rb +17 -0
  38. data/lib/extensions/application_helper_extensions.rb +39 -0
  39. data/lib/extensions/page_extensions.rb +22 -0
  40. data/lib/gemspec.rb +31 -0
  41. data/lib/generators/refinery/snippets_generator.rb +22 -0
  42. data/lib/refinery/snippets.rb +29 -0
  43. data/lib/refinery/snippets/engine.rb +49 -0
  44. data/lib/refinery/snippets/tabs.rb +22 -0
  45. data/lib/refinery/snippets/version.rb +5 -0
  46. data/lib/refinerycms-snippets.rb +1 -0
  47. data/lib/tasks/snippets.rake +13 -0
  48. data/readme.md +52 -0
  49. data/refinerycms-snippets.gemspec +106 -0
  50. data/spec/helpers/application_helper_spec.rb +43 -0
  51. data/spec/models/page_spec.rb +39 -0
  52. data/spec/models/snippet_spec.rb +91 -0
  53. metadata +133 -0
@@ -0,0 +1,20 @@
1
+ class TranslateSnippets < ActiveRecord::Migration
2
+
3
+ def up
4
+ ::Refinery::Snippet.reset_column_information
5
+ unless ::Refinery::Snippet::Translation.table_exists?
6
+ ::Refinery::Snippet.create_translation_table!({
7
+ :body => :text
8
+ }, {
9
+ :migrate_data => true
10
+ })
11
+ end
12
+ end
13
+
14
+ def down
15
+ ::Refinery::Snippet.reset_column_information
16
+
17
+ ::Refinery::Snippet.drop_translation_table!
18
+ end
19
+
20
+ end
@@ -0,0 +1,22 @@
1
+ class CreateSnippetsPageParts < ActiveRecord::Migration
2
+
3
+ def up
4
+ unless ::Refinery::SnippetPagePart.table_exists?
5
+ create_table ::Refinery::SnippetPagePart.table_name do |t|
6
+ t.integer :snippet_id, :null => false, :references => [:snippets, :id]
7
+ t.integer :page_part_id, :null => false, :references => [:page_parts, :id]
8
+ t.integer :position, :null => false, :default => 0
9
+ t.boolean :before_body, :null => false, :default => false
10
+ t.timestamps
11
+ end
12
+ end
13
+
14
+ add_index ::Refinery::SnippetPagePart.table_name, :snippet_id
15
+ add_index ::Refinery::SnippetPagePart.table_name, :page_part_id
16
+ end
17
+
18
+ def down
19
+ drop_table ::Refinery::SnippetPagePart.table_name
20
+ end
21
+
22
+ end
@@ -0,0 +1,12 @@
1
+ class RemovePositionFromSnippets < ActiveRecord::Migration
2
+
3
+ def self.up
4
+ remove_column ::Refinery::Snippet.table_name, :position
5
+ end
6
+
7
+ def self.down
8
+ add_column(::Refinery::Snippet.table_name, :position, :integer,
9
+ :null => :false, :default => 0)
10
+ end
11
+
12
+ end
@@ -0,0 +1,6 @@
1
+ Refinery::User.find(:all).each do |user|
2
+ if user.plugins.where(:name => 'refinery_snippets').blank?
3
+ user.plugins.create(:name => 'refinery_snippets',
4
+ :position => (user.plugins.maximum(:position) || -1) +1)
5
+ end
6
+ end if defined?(Refinery::User)
@@ -0,0 +1,63 @@
1
+ @snippets
2
+ Feature: Snippets
3
+ In order to have snippets on my website
4
+ As an administrator
5
+ I want to manage snippets
6
+
7
+ Background:
8
+ Given I am a logged in refinery user
9
+ And I have no snippets
10
+
11
+ @snippets-list @list
12
+ Scenario: Snippets List
13
+ Given I have snippets titled UniqueTitleOne, UniqueTitleTwo
14
+ When I go to the list of snippets
15
+ Then I should see "UniqueTitleOne"
16
+ And I should see "UniqueTitleTwo"
17
+
18
+ @snippets-valid @valid
19
+ Scenario: Create Valid Snippet
20
+ When I go to the list of snippets
21
+ And I follow "Add New Snippet"
22
+ And I fill in "Title" with "This is a test of the first string field"
23
+ And I press "Save"
24
+ Then I should see "This is a test of the first string field was successfully added."
25
+ And I should have 1 snippet
26
+
27
+ @snippets-invalid @invalid
28
+ Scenario: Create Invalid Snippet (without title)
29
+ When I go to the list of snippets
30
+ And I follow "Add New Snippet"
31
+ And I press "Save"
32
+ Then I should see "Title can't be blank"
33
+ And I should have 0 snippets
34
+
35
+ @snippets-edit @edit
36
+ Scenario: Edit Existing Snippet
37
+ Given I have snippets titled "A title"
38
+ When I go to the list of snippets
39
+ And I follow "Edit this snippet" within ".actions"
40
+ Then I fill in "Title" with "A different title"
41
+ And I press "Save"
42
+ Then I should see "'A different title' was successfully updated."
43
+ And I should be on the list of snippets
44
+ And I should not see "A title"
45
+
46
+ @snippets-duplicate @duplicate
47
+ Scenario: Create Duplicate Snippet
48
+ Given I only have snippets titled UniqueTitleOne, UniqueTitleTwo
49
+ When I go to the list of snippets
50
+ And I follow "Add New Snippet"
51
+ And I fill in "Title" with "UniqueTitleTwo"
52
+ And I press "Save"
53
+ Then I should see "There were problems"
54
+ And I should have 2 snippets
55
+
56
+ @snippets-delete @delete
57
+ Scenario: Delete Snippet
58
+ Given I only have snippets titled UniqueTitleOne
59
+ When I go to the list of snippets
60
+ And I follow "Remove this snippet forever"
61
+ Then I should see "'UniqueTitleOne' was successfully removed."
62
+ And I should have 0 snippets
63
+
@@ -0,0 +1,14 @@
1
+ Given /^I have no snippets$/ do
2
+ Snippet.delete_all
3
+ end
4
+
5
+ Given /^I (only )?have snippets titled "?([^\"]*)"?$/ do |only, titles|
6
+ Snippet.delete_all if only
7
+ titles.split(', ').each do |title|
8
+ Snippet.create(:title => title)
9
+ end
10
+ end
11
+
12
+ Then /^I should have ([0-9]+) snippets?$/ do |count|
13
+ Snippet.count.should == count.to_i
14
+ end
@@ -0,0 +1,17 @@
1
+ module NavigationHelpers
2
+ module Refinery
3
+ module Snippets
4
+ def path_to(page_name)
5
+ case page_name
6
+ when /the list of snippets/
7
+ admin_snippets_path
8
+
9
+ when /the new snippet form/
10
+ new_admin_snippet_path
11
+ else
12
+ nil
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,39 @@
1
+ module Extensions
2
+ module ApplicationHelper
3
+
4
+ def self.included(base)
5
+ base.class_eval do
6
+
7
+ ##
8
+ # Accessor method to get a page part from a page.
9
+ # Example:
10
+ #
11
+ # content_of(Page.first, :body)
12
+ #
13
+ # Will return the body page part of the first page wrap with its
14
+ # attached snippets.
15
+ def content_of(page, part_title)
16
+ part = page.parts.detect do |part|
17
+ part.title.present? and #protecting against the problem that occurs when have nil title
18
+ part.title == part_title.to_s or
19
+ part.title.downcase.gsub(" ", "_") == part_title.to_s.downcase.gsub(" ", "_")
20
+ end
21
+
22
+ if part
23
+ content = ""
24
+ content += part.snippets.before.map{|snippet| content_or_render_of(snippet)}.join
25
+ part_body = part.try(:body)
26
+ content += part_body unless part_body.nil?
27
+ content += part.snippets.after.map{|snippet| content_or_render_of(snippet)}.join
28
+ end
29
+ end
30
+
31
+ def content_or_render_of(snippet)
32
+ snippet.template? ? render(:file => snippet.template_path) : snippet.body
33
+ end
34
+
35
+ end
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,22 @@
1
+ module Extensions
2
+ module Page
3
+
4
+ def self.included(base)
5
+
6
+ base.class_eval do
7
+
8
+ scope :for_snippet, lambda{ |snippet|
9
+ raise RuntimeError.new("Couldn't find Pages for a nil Snippet") if snippet.blank?
10
+ joins(:parts => :snippets).where(:snippets_page_parts => {:snippet_id => snippet.id})
11
+ }
12
+
13
+ def snippets
14
+ Refinery::Snippet.for_page(self)
15
+ end
16
+
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+ require File.expand_path('../refinery/snippets/version', __FILE__)
4
+
5
+ files = Dir.glob("**/*").flatten.reject do |file|
6
+ file =~ /\.gem$/
7
+ end
8
+
9
+ gemspec = <<EOF
10
+ Gem::Specification.new do |s|
11
+ s.platform = Gem::Platform::RUBY
12
+ s.name = %q{refinerycms-sl-snippets}
13
+ s.version = %q{#{Refinery::Snippets::VERSION}}
14
+ s.description = %q{Ruby on Rails Snippets engine for Refinery CMS}
15
+ s.date = %q{#{Time.now.strftime('%Y-%m-%d')}}
16
+ s.summary = %q{Html snippets for Refinery CMS page}
17
+ s.authors = ['Rodrigo García Suárez', 'Simplelogica', 'Marek L.']
18
+ s.email = %q{rodrigo@simplelogica.net}
19
+ s.require_paths = %w(lib)
20
+ s.homepage = %q{https://github.com/simplelogica/refinerycms-snippets}
21
+
22
+ s.add_dependency 'refinerycms-pages', '>= 2.0.0'
23
+
24
+ s.files = [
25
+ '#{files.join("',\n '")}'
26
+ ]
27
+ s.require_path = 'lib'
28
+ end
29
+ EOF
30
+
31
+ File.open(File.expand_path("../../refinerycms-snippets.gemspec", __FILE__), 'w').puts(gemspec)
@@ -0,0 +1,22 @@
1
+ module Refinery
2
+ class SnippetsGenerator < Rails::Generators::Base
3
+
4
+ def rake_db
5
+ rake("refinery_snippets:install:migrations")
6
+ end
7
+
8
+ source_root File.expand_path('../templates', __FILE__)
9
+
10
+ def append_load_seed_data
11
+ append_file 'db/seeds.rb', :verbose => true do
12
+ <<-EOH
13
+
14
+ # Added by RefineryCMS Snippets engine
15
+ Refinery::Snippets::Engine.load_seed
16
+ EOH
17
+ end
18
+ end
19
+
20
+
21
+ end
22
+ end
@@ -0,0 +1,29 @@
1
+ require 'refinerycms-pages'
2
+
3
+ module Refinery
4
+ autoload :SnippetsGenerator, 'generators/refinery/snippets_generator'
5
+
6
+ module Snippets
7
+ require 'refinery/snippets/engine' if defined?(Rails)
8
+
9
+ autoload :Version, 'refinery/snippets/version'
10
+ autoload :Tab, 'refinery/snippets/tabs'
11
+
12
+ class << self
13
+ attr_accessor :root
14
+ attr_writer :tabs
15
+
16
+ def root
17
+ @root ||= Pathname.new(File.expand_path('../../', __FILE__))
18
+ end
19
+
20
+ def tabs
21
+ @tabs ||= []
22
+ end
23
+
24
+ def version
25
+ ::Refinery::Snippets::Version.to_s
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,49 @@
1
+ require 'refinerycms-snippets'
2
+
3
+ module Refinery
4
+ module Snippets
5
+ class Engine < Rails::Engine
6
+ include Refinery::Engine
7
+
8
+ isolate_namespace Refinery
9
+ engine_name :refinery_snippets
10
+
11
+ config.before_initialize do
12
+ require 'extensions/page_extensions'
13
+ require 'extensions/application_helper_extensions'
14
+ end
15
+
16
+ initializer "register refinery_snippets plugin", :after => :set_routes_reloader do |app|
17
+
18
+ Refinery::Plugin.register do |plugin|
19
+ plugin.pathname = root
20
+ plugin.name = "refinery_snippets"
21
+ plugin.url = proc {Refinery::Core::Engine.routes.url_helpers.admin_snippets_path}
22
+ plugin.menu_match = /^\/?(admin|refinery)\/snippets/
23
+ plugin.activity = {
24
+ :class_name => :'refinery/snippet',
25
+ :title => 'title'
26
+ }
27
+ end
28
+ end
29
+
30
+ config.to_prepare do
31
+ Refinery::PagePart.module_eval do
32
+ has_many :snippet_page_parts, :dependent => :destroy
33
+ has_many :snippets, :through => :snippet_page_parts, :order => 'position ASC'
34
+ end
35
+ Refinery::Page.send :include, Extensions::Page
36
+ ApplicationHelper.send :include, Extensions::ApplicationHelper
37
+ end
38
+
39
+ config.after_initialize do
40
+ ::Refinery::Pages::Tab.register do |tab|
41
+ tab.name = "snippets"
42
+ tab.partial = "/refinery/admin/pages/tabs/snippets"
43
+ end
44
+
45
+ Refinery.register_engine(Refinery::Snippets)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,22 @@
1
+ module Refinery
2
+ module Snippets
3
+ class Tab
4
+ attr_accessor :name, :partial
5
+
6
+ def self.register(&block)
7
+ tab = self.new
8
+
9
+ yield tab
10
+
11
+ raise "A tab MUST have a name!: #{tab.inspect}" if tab.name.blank?
12
+ raise "A tab MUST have a partial!: #{tab.inspect}" if tab.partial.blank?
13
+ end
14
+
15
+ protected
16
+
17
+ def initialize
18
+ ::Refinery::Snippets.tabs << self # add me to the collection of registered page tabs
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ module Refinery
2
+ module Snippets
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require 'refinery/snippets'
@@ -0,0 +1,13 @@
1
+ namespace :refinery do
2
+
3
+ namespace :snippets do
4
+
5
+ # call this task my running: rake refinery:snippets:my_task
6
+ # desc "Description of my task below"
7
+ # task :my_task => :environment do
8
+ # # add your logic here
9
+ # end
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1,52 @@
1
+ # Snippets engine for Refinery CMS.
2
+
3
+ ## About
4
+
5
+ Snippets allows you to relate one or more html blocks or erb templates to any page in Refinery. These are attached to the page parts, before or after the its html body.
6
+
7
+ ## Requirements
8
+
9
+ * RefineryCMS with 'Pages' engine (refinerycms-pages >= 2.0.0)
10
+
11
+ ## TODO
12
+
13
+ ### to 1.5
14
+ * improve UI
15
+ * Documentation
16
+ * Tests
17
+
18
+ ## Install
19
+
20
+ Add this line to your applications `Gemfile`
21
+
22
+ gem 'refinerycms-sl-snippets', '~> 1.0.0', :require => 'refinerycms-snippets'
23
+
24
+ Next run
25
+
26
+ bundle install
27
+ rails g refinerycms:snippets
28
+ rake db:migrate
29
+
30
+ ## Usage
31
+
32
+ * Create Snippet on /refinery/snippets
33
+ * Now you can attach snippet to page when you click Edit this page on `/refinery/pages`. In the Snippets tab you can select the part to which you want to attach the block and add it after and/or before the html body of the part.
34
+ * Next you can use content_of(@page, :part) to print the body of the part and the snippets attached to it in the pages views.
35
+ * You have some other interesting methods to work with snippets:
36
+ * content_or_render_of(snippet): will return the content body (or erb template) of the snippet.
37
+ * page.snippets: returns all the snippets attached to all the parts of page.
38
+ * snippet.pages: returns all pages to whose parts is snippet attached.
39
+ * snippet.before?(part): returns true if snippet is attached before part body.
40
+ * snippet.after?(part): return true if snippet is attached after part body.
41
+
42
+ ## ERB templates usage
43
+
44
+ Snippets search for templates in RAILS_VIEWS_PATH/shared/snippets. The snippet searchs for a file with its same name but with all non A-Za-z0-9 characters changed to underscores. If this file exists, snippet html body is ignored and the template is rendered in its place. Methods rendering the template, if exists, in place of the body are content_of(@page, :part) and content_or_render_of(snippet).
45
+
46
+ For example, a template with title "VIP clients: photos" would search in "app/views/shared/snippets" for a file named "vip_clients_photos.html.erb"; "Country & region banners: España" for "country_region_banners_espa_na" and so on.
47
+
48
+ Templates are normal erb files. If you want to add complex logic to them I suggest the use of [Cells](http://cells.rubyforge.org/).
49
+
50
+ ## Thanks
51
+
52
+ This is build upon the original refinerycms-snippets from keram: https://github.com/keram/refinerycms-snippets