refinerycms-snippets 0.2 → 0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/app/controllers/admin/snippets_controller.rb +1 -1
- data/app/controllers/admin/snippets_pages_controller.rb +31 -0
- data/app/models/snippet.rb +10 -1
- data/app/models/snippet_page.rb +9 -5
- data/app/views/admin/pages/tabs/_snippets.html.erb +5 -10
- data/app/views/admin/pages/tabs/_snippets_bar.html.erb +10 -7
- data/app/views/admin/pages/tabs/_snippets_content.html.erb +2 -0
- data/app/views/admin/pages/tabs/_snippets_field.html.erb +27 -2
- data/app/views/admin/snippets/_form.html.erb +1 -1
- data/app/views/admin/snippets_pages/index.html.erb +2 -0
- data/config/routes.rb +4 -1
- data/db/migrate/1_create_snippets.rb +5 -5
- data/db/migrate/2_create_snippets_pages.rb +18 -0
- data/db/seeds/snippets.rb +13 -11
- data/lib/refinerycms-snippets.rb +8 -4
- metadata +7 -4
- data/db/migrate/2_create_page_snippets.rb +0 -18
@@ -0,0 +1,31 @@
|
|
1
|
+
module Admin
|
2
|
+
class SnippetsPagesController < Admin::BaseController
|
3
|
+
def index
|
4
|
+
|
5
|
+
@page = Page.find(params[:page_id])
|
6
|
+
@do = 'nothing'
|
7
|
+
|
8
|
+
if params[:add].to_i > 0
|
9
|
+
@do = 'add'
|
10
|
+
sp = SnippetPage.new(:page => @page, :snippet => Snippet.find(params[:add].to_i))
|
11
|
+
|
12
|
+
if sp.save
|
13
|
+
flash[:notice] = 'Snippet was successfully added to page.'
|
14
|
+
end
|
15
|
+
else
|
16
|
+
if params[:remove].to_i > 0
|
17
|
+
@do = 'remove'
|
18
|
+
sp = SnippetPage.where(:page_id => @page.id, :snippet_id => params[:remove].to_i)
|
19
|
+
removed = sp.first.delete() unless sp.empty?
|
20
|
+
|
21
|
+
if removed
|
22
|
+
flash[:notice] = 'Snippet was successfully removed from page.'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
render :layout => false if request.xhr?
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/app/models/snippet.rb
CHANGED
@@ -1,11 +1,20 @@
|
|
1
1
|
class Snippet < ActiveRecord::Base
|
2
|
-
|
2
|
+
|
3
3
|
acts_as_indexed :fields => [:title, :body]
|
4
4
|
|
5
5
|
validates :title, :presence => true, :uniqueness => true
|
6
6
|
|
7
7
|
translates :body
|
8
8
|
|
9
|
+
# has_many :pages, :through => :snippets_pages
|
10
|
+
|
11
|
+
def self.inactive(page)
|
12
|
+
@page = page
|
13
|
+
snippets = scoped
|
14
|
+
snippets = snippets.where('id NOT IN (?)', @page.snippets) unless @page.snippets.empty?
|
15
|
+
snippets
|
16
|
+
end
|
17
|
+
|
9
18
|
# rejects any page that has not been translated to the current locale.
|
10
19
|
scope :translated, lambda {
|
11
20
|
pages = Arel::Table.new(Snippet.table_name)
|
data/app/models/snippet_page.rb
CHANGED
@@ -1,10 +1,14 @@
|
|
1
1
|
class SnippetPage < ActiveRecord::Base
|
2
|
-
|
3
|
-
|
4
|
-
|
2
|
+
|
3
|
+
set_table_name 'snippets_pages'
|
4
|
+
|
5
|
+
belongs_to :snippet, :foreign_key => :snippet_id, :dependent => :destroy
|
6
|
+
belongs_to :page, :foreign_key => :page_id, :dependent => :destroy
|
7
|
+
|
8
|
+
validates_uniqueness_of(:snippet_id, :scope => :page_id)
|
5
9
|
|
6
10
|
before_save do |snippet_page|
|
7
|
-
snippet_page.position = (SnippetPage.maximum(:position) || -1) + 1
|
11
|
+
snippet_page.position = (SnippetPage.where('page_id = ?', snippet_page.page_id).maximum(:position) || -1) + 1
|
8
12
|
end
|
9
|
-
|
13
|
+
|
10
14
|
end
|
@@ -1,12 +1,7 @@
|
|
1
|
-
<div class='wym_skin_refinery page_part' id='
|
2
|
-
<%= render :partial => '/admin/pages/tabs/
|
3
|
-
<%= render :partial => '/admin/pages/tabs/snippets_field', :locals => {:f => f} %>
|
1
|
+
<div class='wym_skin_refinery page_part' id='page_snippet_picker' style="padding: 5px;">
|
2
|
+
<%= render :partial => '/admin/pages/tabs/snippets_content', :locals => {:f => f} %>
|
4
3
|
</div>
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
<%# content_for :javascripts do %>
|
11
|
-
<%#= javascript_include_tag 'page-image-picker' %>
|
12
|
-
<%# end %>
|
5
|
+
<% content_for :javascripts do %>
|
6
|
+
<%= javascript_include_tag 'page-snippet-picker' %>
|
7
|
+
<% end %>
|
@@ -1,9 +1,12 @@
|
|
1
1
|
<div class='wym_area_top'>
|
2
|
-
<
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
2
|
+
<div style="padding: 5px 0;">
|
3
|
+
<%
|
4
|
+
|
5
|
+
inactive_snippets = Snippet.inactive(@page).map { |snippet| [snippet.title, snippet.id] }
|
6
|
+
inactive_snippets.unshift(['Select snippet', ''])
|
7
|
+
|
8
|
+
%>
|
9
|
+
<%= select_tag(:inactive_snippets, options_for_select(inactive_snippets)) %>
|
10
|
+
<a href="<%= admin_snippets_pages_path(:page_id => @page.id.to_s) %>" id="add-snippet" class="add_icon" style="background-repeat: no-repeat; background-position: left center;border: none; padding: 2px 12px 2px 20px">Add</a>
|
11
|
+
</div>
|
9
12
|
</div>
|
@@ -1,3 +1,28 @@
|
|
1
|
-
<div class="wym_box field snippets_field" >
|
2
|
-
|
1
|
+
<div class="wym_box field snippets_field" id="snippets-holder">
|
2
|
+
<% if @page.snippets.any? %>
|
3
|
+
<ul style="padding: 10px; background: #fff; margin: 5px 0;">
|
4
|
+
<% @page.snippets.each do |snippet| %>
|
5
|
+
<li class='clearfix record <%= cycle("on", "on-hover") %>' id="<%= dom_id(snippet) -%>">
|
6
|
+
<span class='title'>
|
7
|
+
<%= snippet.title %>
|
8
|
+
<% if ::Refinery::I18n.frontend_locales.many? and
|
9
|
+
(locales = snippet.translations.collect{|t| t.locale}).present? %>
|
10
|
+
<span class='preview'>
|
11
|
+
<% locales.each do |locale| %>
|
12
|
+
<%= refinery_icon_tag "flags/#{locale}.png", :size => '16x11' %>
|
13
|
+
<% end %>
|
14
|
+
</span>
|
15
|
+
<% end %>
|
16
|
+
</span>
|
17
|
+
<span class='actions'>
|
18
|
+
<%= link_to refinery_icon_tag("application_edit.png"), edit_admin_snippet_path(snippet),
|
19
|
+
:title => t('.edit') %>
|
20
|
+
<%= link_to refinery_icon_tag("delete.png"), admin_snippets_pages_path(:page_id => @page.id, 'remove' => snippet.id),
|
21
|
+
:title => t('.remove'),
|
22
|
+
:class => 'remove-snippet' %>
|
23
|
+
</span>
|
24
|
+
</li>
|
25
|
+
<% end %>
|
26
|
+
</ul>
|
27
|
+
<% end %>
|
3
28
|
</div>
|
@@ -3,6 +3,7 @@
|
|
3
3
|
:object => @snippet,
|
4
4
|
:include_object_name => true
|
5
5
|
} %>
|
6
|
+
|
6
7
|
|
7
8
|
<%= render :partial => "locale_picker",
|
8
9
|
:locals => {
|
@@ -13,7 +14,6 @@
|
|
13
14
|
<%= f.label :title -%>
|
14
15
|
<%= f.text_field :title, :class => 'larger widest' -%>
|
15
16
|
</div>
|
16
|
-
|
17
17
|
<div class='field'>
|
18
18
|
<div id='page-tabs' class='clearfix ui-tabs ui-widget ui-widget-content ui-corner-all'>
|
19
19
|
<ul id='page_parts'>
|
data/config/routes.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
Refinery::Application.routes.draw do
|
2
|
-
resources :snippets, :only => [:index, :show]
|
2
|
+
resources :snippets, :only => [:index, :show]
|
3
3
|
|
4
4
|
scope(:path => 'refinery', :as => 'admin', :module => 'admin') do
|
5
5
|
resources :snippets, :except => :show do
|
@@ -7,5 +7,8 @@ Refinery::Application.routes.draw do
|
|
7
7
|
post :update_positions
|
8
8
|
end
|
9
9
|
end
|
10
|
+
|
11
|
+
resources :snippets_pages
|
10
12
|
end
|
13
|
+
|
11
14
|
end
|
@@ -2,14 +2,14 @@ class CreateSnippets < ActiveRecord::Migration
|
|
2
2
|
|
3
3
|
def self.up
|
4
4
|
create_table :snippets do |t|
|
5
|
-
t.string :title
|
5
|
+
t.string :title, :limit => 36, :null => false
|
6
6
|
t.text :body
|
7
|
-
t.integer :position
|
8
|
-
|
7
|
+
t.integer :position, :null => false, :default => 0
|
9
8
|
t.timestamps
|
10
9
|
end
|
11
10
|
|
12
|
-
|
11
|
+
# we have primary key, thus we don't need duplicate index with them
|
12
|
+
# add_index :snippets, :id
|
13
13
|
|
14
14
|
load(Rails.root.join('db', 'seeds', 'snippets.rb'))
|
15
15
|
end
|
@@ -17,7 +17,7 @@ class CreateSnippets < ActiveRecord::Migration
|
|
17
17
|
def self.down
|
18
18
|
UserPlugin.destroy_all({:name => "snippets"})
|
19
19
|
|
20
|
-
|
20
|
+
Page.delete_all({:link_url => "/snippets"})
|
21
21
|
|
22
22
|
drop_table :snippets
|
23
23
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class CreateSnippetsPages < ActiveRecord::Migration
|
2
|
+
|
3
|
+
def self.up
|
4
|
+
create_table :snippets_pages do |t|
|
5
|
+
t.integer :snippet_id, :null => false, :references => [:snippets, :id]
|
6
|
+
t.integer :page_id, :null => false, :references => [:pages, :id]
|
7
|
+
t.integer :position, :null => false, :default => 0
|
8
|
+
end
|
9
|
+
|
10
|
+
# add_index :snippets_pages, :snippet_id
|
11
|
+
add_index :snippets_pages, :page_id
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.down
|
15
|
+
drop_table :snippets_pages
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
data/db/seeds/snippets.rb
CHANGED
@@ -5,14 +5,16 @@ User.all.each do |user|
|
|
5
5
|
end
|
6
6
|
end
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
:
|
11
|
-
:
|
12
|
-
:
|
13
|
-
:
|
14
|
-
:
|
15
|
-
)
|
16
|
-
|
17
|
-
|
18
|
-
|
8
|
+
# we don't need this on frontend
|
9
|
+
#page = Page.create(
|
10
|
+
# :title => 'Snippets',
|
11
|
+
# :link_url => '/snippets',
|
12
|
+
# :show_in_menu => false,
|
13
|
+
# :deletable => false,
|
14
|
+
# :position => ((Page.maximum(:position, :conditions => {:parent_id => nil}) || -1)+1),
|
15
|
+
# :menu_match => '^/snippets(\/|\/.+?|)$'
|
16
|
+
#)
|
17
|
+
#
|
18
|
+
#Page.default_parts.each do |default_page_part|
|
19
|
+
# page.parts.create(:title => default_page_part, :body => nil)
|
20
|
+
#end
|
data/lib/refinerycms-snippets.rb
CHANGED
@@ -9,8 +9,8 @@ module Refinery
|
|
9
9
|
|
10
10
|
config.to_prepare do
|
11
11
|
Page.module_eval do
|
12
|
-
has_many :
|
13
|
-
has_many :snippets, :through => :
|
12
|
+
has_many :snippet_page
|
13
|
+
has_many :snippets, :through => :snippet_page, :order => 'position ASC'
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
@@ -21,9 +21,13 @@ module Refinery
|
|
21
21
|
end
|
22
22
|
Refinery::Plugin.register do |plugin|
|
23
23
|
plugin.name = "snippets"
|
24
|
-
plugin.
|
24
|
+
plugin.url = {:controller => '/admin/snippets'}
|
25
|
+
plugin.menu_match = /^\/?(admin|refinery)\/snippets/
|
26
|
+
plugin.activity = [{
|
25
27
|
:class => Snippet
|
26
|
-
|
28
|
+
}, {
|
29
|
+
:class => SnippetPage
|
30
|
+
}]
|
27
31
|
end
|
28
32
|
end
|
29
33
|
end
|
metadata
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: refinerycms-snippets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 13
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: "0.
|
8
|
+
- 3
|
9
|
+
version: "0.3"
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Marek L.
|
@@ -37,6 +37,7 @@ files:
|
|
37
37
|
- config/locales/nl.yml
|
38
38
|
- app/controllers/snippets_controller.rb
|
39
39
|
- app/controllers/admin/snippets_controller.rb
|
40
|
+
- app/controllers/admin/snippets_pages_controller.rb
|
40
41
|
- app/models/snippet_page.rb
|
41
42
|
- app/models/snippet.rb
|
42
43
|
- app/views/snippets/show.html.erb
|
@@ -53,8 +54,10 @@ files:
|
|
53
54
|
- app/views/admin/snippets/_snippet.html.erb
|
54
55
|
- app/views/admin/pages/tabs/_snippets.html.erb
|
55
56
|
- app/views/admin/pages/tabs/_snippets_bar.html.erb
|
57
|
+
- app/views/admin/pages/tabs/_snippets_content.html.erb
|
56
58
|
- app/views/admin/pages/tabs/_snippets_field.html.erb
|
57
|
-
-
|
59
|
+
- app/views/admin/snippets_pages/index.html.erb
|
60
|
+
- db/migrate/2_create_snippets_pages.rb
|
58
61
|
- db/migrate/1_create_snippets.rb
|
59
62
|
- db/migrate/3_translate_snippets.rb
|
60
63
|
- db/seeds/snippets.rb
|
@@ -1,18 +0,0 @@
|
|
1
|
-
class CreatePageSnippets < ActiveRecord::Migration
|
2
|
-
|
3
|
-
def self.up
|
4
|
-
create_table :snippet_pages, :id => false do |t|
|
5
|
-
t.integer :snippet_id
|
6
|
-
t.integer :page_id
|
7
|
-
t.integer :position
|
8
|
-
end
|
9
|
-
|
10
|
-
add_index :snippet_pages, :snippet_id
|
11
|
-
add_index :snippet_pages, :page_id
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.down
|
15
|
-
drop_table :snippet_pages
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|