puffer_pages 0.1.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/.rspec +3 -1
- data/.rvmrc +1 -1
- data/.travis.yml +13 -5
- data/CHANGELOG.md +130 -7
- data/Gemfile +10 -1
- data/README.md +18 -20
- data/Rakefile +1 -1
- data/app/assets/javascripts/puffer/codemirror.js +4237 -2223
- data/app/assets/javascripts/puffer/codemirror/css.js +359 -18
- data/app/assets/javascripts/puffer/codemirror/htmlmixed.js +15 -14
- data/app/assets/javascripts/puffer/codemirror/javascript.js +91 -29
- data/app/assets/javascripts/puffer/codemirror/liquid.js +185 -0
- data/app/assets/javascripts/puffer/codemirror/xml.js +99 -27
- data/app/assets/javascripts/puffer/codemirror/yaml.js +95 -0
- data/app/assets/javascripts/puffer/liquid.js +28 -12
- data/app/assets/javascripts/puffer/matchbrackets.js +63 -0
- data/app/assets/javascripts/puffer/multiplex.js +95 -0
- data/app/assets/javascripts/puffer/puffer_pages.js +140 -44
- data/app/assets/stylesheets/puffer/codemirror.css +203 -67
- data/app/assets/stylesheets/puffer/codemirror/ambiance-mobile.css +6 -0
- data/app/assets/stylesheets/puffer/codemirror/ambiance.css +76 -0
- data/app/assets/stylesheets/puffer/codemirror/blackboard.css +25 -0
- data/app/assets/stylesheets/puffer/codemirror/cobalt.css +4 -4
- data/app/assets/stylesheets/puffer/codemirror/eclipse.css +1 -1
- data/app/assets/stylesheets/puffer/codemirror/elegant.css +2 -2
- data/app/assets/stylesheets/puffer/codemirror/erlang-dark.css +21 -0
- data/app/assets/stylesheets/puffer/codemirror/lesser-dark.css +44 -0
- data/app/assets/stylesheets/puffer/codemirror/monokai.css +4 -4
- data/app/assets/stylesheets/puffer/codemirror/neat.css +3 -3
- data/app/assets/stylesheets/puffer/codemirror/night.css +4 -4
- data/app/assets/stylesheets/puffer/codemirror/rubyblue.css +5 -5
- data/app/assets/stylesheets/puffer/codemirror/solarized.css +207 -0
- data/app/assets/stylesheets/puffer/codemirror/twilight.css +26 -0
- data/app/assets/stylesheets/puffer/codemirror/vibrant-ink.css +27 -0
- data/app/assets/stylesheets/puffer/codemirror/xq-dark.css +46 -0
- data/app/assets/stylesheets/puffer/puffer_pages.css +64 -35
- data/app/components/codemirror/form.html.erb +22 -6
- data/app/components/codemirror_component.rb +1 -8
- data/app/components/handlers/form.html.erb +8 -0
- data/app/components/handlers_component.rb +8 -0
- data/app/components/page_parts/_page_part.html.erb +6 -0
- data/app/components/page_parts/form.html.erb +8 -27
- data/app/components/page_parts_component.rb +1 -3
- data/app/components/render/_tree_page.html.erb +3 -2
- data/app/controllers/admin/layouts_controller.rb +1 -1
- data/app/controllers/admin/origins_controller.rb +3 -0
- data/app/controllers/admin/pages_controller.rb +1 -1
- data/app/controllers/admin/snippets_controller.rb +1 -1
- data/app/controllers/pages_controller.rb +3 -3
- data/app/helpers/puffer_pages_helper.rb +3 -3
- data/app/models/puffer_pages/layout.rb +2 -7
- data/app/models/puffer_pages/origin.rb +2 -0
- data/app/models/puffer_pages/page.rb +1 -117
- data/app/models/puffer_pages/page_part.rb +2 -22
- data/app/models/puffer_pages/snippet.rb +2 -5
- data/config/routes.rb +3 -0
- data/db/migrate/20120812100913_create_origins.rb +16 -0
- data/db/migrate/20120924120226_migrate_to_uuid.rb +126 -0
- data/db/migrate/20130110144030_add_handler_to_page_parts.rb +9 -0
- data/db/migrate/20130118064524_add_locales_to_pages.rb +9 -0
- data/gemfiles/Gemfile.rails-3-1 +7 -0
- data/gemfiles/Gemfile.rails-3-2 +7 -0
- data/gemfiles/Gemfile.rails-head +7 -0
- data/lib/puffer_pages.rb +43 -7
- data/lib/puffer_pages/backends.rb +16 -0
- data/{app/controllers/puffer_pages → lib/puffer_pages/backends/controllers}/layouts_base.rb +2 -5
- data/lib/puffer_pages/backends/controllers/origins_base.rb +43 -0
- data/lib/puffer_pages/backends/controllers/pages_base.rb +52 -0
- data/{app/controllers/puffer_pages → lib/puffer_pages/backends/controllers}/snippets_base.rb +2 -5
- data/lib/puffer_pages/backends/models/layout.rb +34 -0
- data/lib/puffer_pages/backends/models/mixins/importable.rb +42 -0
- data/lib/puffer_pages/backends/models/mixins/localable.rb +65 -0
- data/lib/puffer_pages/backends/models/mixins/renderable.rb +153 -0
- data/lib/puffer_pages/backends/models/mixins/translatable.rb +52 -0
- data/lib/puffer_pages/backends/models/origin.rb +59 -0
- data/lib/puffer_pages/backends/models/page.rb +221 -0
- data/lib/puffer_pages/backends/models/page_part.rb +71 -0
- data/lib/puffer_pages/backends/models/snippet.rb +34 -0
- data/lib/puffer_pages/engine.rb +13 -2
- data/lib/puffer_pages/extensions/context.rb +31 -0
- data/lib/puffer_pages/extensions/core.rb +1 -8
- data/lib/puffer_pages/extensions/pagenator.rb +69 -0
- data/lib/puffer_pages/extensions/renderer.rb +31 -0
- data/lib/puffer_pages/globalize/migrator.rb +23 -0
- data/lib/puffer_pages/handlers.rb +29 -0
- data/lib/puffer_pages/handlers/base.rb +21 -0
- data/lib/puffer_pages/handlers/yaml.rb +20 -0
- data/lib/puffer_pages/helpers.rb +14 -0
- data/lib/puffer_pages/liquid/backend.rb +16 -0
- data/lib/puffer_pages/liquid/file_system.rb +36 -12
- data/lib/puffer_pages/liquid/page_drop.rb +26 -23
- data/lib/puffer_pages/liquid/tags/array.rb +39 -0
- data/lib/puffer_pages/liquid/tags/{javascripts.rb → assets.rb} +13 -4
- data/lib/puffer_pages/liquid/tags/helper.rb +15 -0
- data/lib/puffer_pages/liquid/tags/include.rb +32 -0
- data/lib/puffer_pages/liquid/tags/javascript.rb +15 -0
- data/lib/puffer_pages/liquid/tags/partials.rb +22 -0
- data/lib/puffer_pages/liquid/tags/render.rb +28 -0
- data/lib/puffer_pages/liquid/tags/scope.rb +31 -0
- data/lib/puffer_pages/liquid/tags/super.rb +36 -0
- data/lib/puffer_pages/liquid/tags/translate.rb +56 -0
- data/lib/puffer_pages/liquid/tags/url.rb +53 -0
- data/lib/puffer_pages/liquid/tags/yield.rb +3 -1
- data/lib/puffer_pages/log_subscriber.rb +29 -0
- data/lib/puffer_pages/migrations.rb +31 -0
- data/lib/puffer_pages/renderer.rb +25 -0
- data/lib/puffer_pages/rspec/matchers.rb +27 -0
- data/lib/puffer_pages/rspec/matchers/render_page.rb +90 -0
- data/lib/puffer_pages/version.rb +1 -1
- data/puffer_pages.gemspec +18 -18
- data/spec/controllers/pages_controller_spec.rb +44 -0
- data/spec/controllers/puffer_pages_controller_spec.rb +194 -0
- data/spec/data/broken.json +8 -0
- data/spec/data/import.json +55 -0
- data/spec/data/localized.json +106 -0
- data/spec/data/unlocalized.json +82 -0
- data/spec/dummy/.rvmrc +1 -0
- data/spec/dummy/app/controllers/application_controller.rb +6 -0
- data/spec/dummy/app/views/layouts/sample.en.erb +1 -0
- data/spec/dummy/app/views/layouts/sample.ru.erb +1 -0
- data/spec/dummy/app/views/shared/_first.html.erb +1 -0
- data/spec/dummy/config/application.rb +21 -2
- data/spec/dummy/config/boot.rb +0 -2
- data/spec/dummy/config/database.yml +7 -1
- data/spec/dummy/config/environments/development.rb +11 -2
- data/spec/dummy/config/environments/pg_test.rb +6 -6
- data/spec/dummy/config/environments/production.rb +19 -3
- data/spec/dummy/config/environments/test.rb +6 -6
- data/spec/dummy/config/initializers/inflections.rb +5 -0
- data/spec/dummy/config/initializers/puffer_pages.rb +4 -0
- data/spec/dummy/config/initializers/secret_token.rb +1 -1
- data/spec/dummy/config/initializers/wrap_parameters.rb +5 -3
- data/spec/dummy/config/routes.rb +2 -1
- data/spec/dummy/db/migrate/{20090422092419_create_pages.rb → 20130118071511_create_pages.rb} +1 -0
- data/spec/dummy/db/migrate/{20090504132337_create_page_parts.rb → 20130118071512_create_page_parts.rb} +1 -0
- data/spec/dummy/db/migrate/{20090506102004_create_layouts.rb → 20130118071513_create_layouts.rb} +1 -0
- data/spec/dummy/db/migrate/{20090510121824_create_snippets.rb → 20130118071514_create_snippets.rb} +1 -0
- data/spec/dummy/db/migrate/20130118071515_create_origins.rb +17 -0
- data/spec/dummy/db/migrate/20130118071516_migrate_to_uuid.rb +127 -0
- data/spec/dummy/db/migrate/20130118071517_add_handler_to_page_parts.rb +10 -0
- data/spec/dummy/db/migrate/20130118071518_add_locales_to_pages.rb +10 -0
- data/spec/dummy/db/migrate/20130118071519_add_translations.rb +9 -0
- data/spec/dummy/db/schema.rb +65 -17
- data/spec/fabricators/articles_fabricator.rb +2 -2
- data/spec/fabricators/layouts_fabricator.rb +24 -3
- data/spec/fabricators/origin_fabricator.rb +5 -0
- data/spec/fabricators/page_parts_fabricator.rb +17 -2
- data/spec/fabricators/pages_fabricator.rb +20 -5
- data/spec/fabricators/snippets_fabricator.rb +14 -3
- data/spec/lib/handlers/base_spec.rb +10 -0
- data/spec/lib/handlers/yaml_spec.rb +34 -0
- data/spec/lib/handlers_spec.rb +36 -0
- data/spec/lib/liquid/backend_spec.rb +20 -0
- data/spec/lib/liquid/interpolation_spec.rb +25 -0
- data/spec/lib/liquid/tags/include_spec.rb +38 -0
- data/spec/lib/liquid/tags/partials_spec.rb +39 -0
- data/spec/lib/liquid/tags/scope_spec.rb +18 -0
- data/spec/lib/liquid/tags/translate_spec.rb +107 -0
- data/spec/lib/liquid/tags_spec.rb +81 -0
- data/spec/lib/page_drop_spec.rb +65 -0
- data/spec/lib/pagenator_spec.rb +176 -0
- data/spec/lib/rspec/matchers/render_page_spec.rb +107 -0
- data/spec/models/puffer_pages/layout_spec.rb +15 -0
- data/spec/models/puffer_pages/localable_spec.rb +62 -0
- data/spec/models/puffer_pages/origin_spec.rb +112 -0
- data/spec/models/puffer_pages/page_part_spec.rb +14 -0
- data/spec/models/puffer_pages/page_spec.rb +307 -0
- data/spec/models/puffer_pages/renderable_spec.rb +84 -0
- data/spec/models/puffer_pages/snippet_spec.rb +15 -0
- data/spec/requests/origins_requests_spec.rb +37 -0
- data/spec/spec_helper.rb +7 -6
- metadata +349 -87
- data/Gemfile.lock +0 -176
- data/app/assets/javascripts/puffer/overlay.js +0 -51
- data/app/controllers/puffer_pages/pages_base.rb +0 -38
- data/app/models/layout.rb +0 -2
- data/app/models/page.rb +0 -2
- data/app/models/page_part.rb +0 -2
- data/app/models/snippet.rb +0 -2
- data/app/views/layouts/puffer_pages_layout.html.erb +0 -2
- data/config/puffer_routes.rb +0 -3
- data/gemfiles/Gemfile-rails-3.1 +0 -5
- data/gemfiles/Gemfile-rails-3.1.lock +0 -173
- data/gemfiles/Gemfile-rails-3.2 +0 -5
- data/gemfiles/Gemfile-rails-3.2.lock +0 -171
- data/lib/puffer_pages/extensions/controller.rb +0 -30
- data/lib/puffer_pages/extensions/mapper.rb +0 -23
- data/lib/puffer_pages/liquid/tags/page_attribute.rb +0 -39
- data/lib/puffer_pages/liquid/tags/stylesheets.rb +0 -38
- data/spec/controllers/articles_controller_spec.rb +0 -65
- data/spec/dummy/app/controllers/articles_controller.rb +0 -9
- data/spec/integration/navigation_spec.rb +0 -9
- data/spec/lib/drops_spec.rb +0 -55
- data/spec/lib/tags_spec.rb +0 -98
- data/spec/models/page_spec.rb +0 -235
- data/spec/puffer_pages_spec.rb +0 -7
@@ -0,0 +1,52 @@
|
|
1
|
+
module PufferPages
|
2
|
+
module Backends
|
3
|
+
module Mixins
|
4
|
+
module Translatable
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def translatable *fields
|
9
|
+
if PufferPages.localize
|
10
|
+
translates *fields, fallbacks_for_empty_translations: true
|
11
|
+
translation_class.send(:include, ActiveUUID::UUID)
|
12
|
+
|
13
|
+
def self.globalize_migrator
|
14
|
+
@globalize_migrator ||= PufferPages::Globalize::Migrator.new(self)
|
15
|
+
end
|
16
|
+
|
17
|
+
fields.each do |field|
|
18
|
+
define_method "#{field}_translations" do
|
19
|
+
result = translations.each_with_object(HashWithIndifferentAccess.new) do |translation, result|
|
20
|
+
result[translation.locale] = translation.send(field)
|
21
|
+
end
|
22
|
+
globalize.stash.keys.each_with_object(result) do |locale, result|
|
23
|
+
result[locale] = globalize.stash.read(locale, field) if globalize.stash.contains?(locale, field)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
define_method "#{field}_translations=" do |value|
|
28
|
+
value.each do |(locale, value)|
|
29
|
+
write_attribute(field, value, locale: locale)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
define_method :serializable_hash_with_translations do |options = nil|
|
35
|
+
options ||= {}
|
36
|
+
except = Array.wrap(options[:except])
|
37
|
+
options[:except] = except +
|
38
|
+
self.class.translated_attribute_names.map(&:to_s)
|
39
|
+
methods = Array.wrap(options[:methods])
|
40
|
+
options[:methods] = methods +
|
41
|
+
self.class.translated_attribute_names.map { |name| "#{name}_translations" }
|
42
|
+
serializable_hash_without_translations options
|
43
|
+
end
|
44
|
+
|
45
|
+
alias_method_chain :serializable_hash, :translations
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
class PufferPages::Backends::Origin < ActiveRecord::Base
|
2
|
+
self.abstract_class = true
|
3
|
+
self.table_name = :origins
|
4
|
+
|
5
|
+
attr_protected
|
6
|
+
|
7
|
+
validates_presence_of :name, :host, :token
|
8
|
+
validates_uniqueness_of :name
|
9
|
+
|
10
|
+
def path
|
11
|
+
read_attribute(:path).presence || PufferPages.export_path
|
12
|
+
end
|
13
|
+
|
14
|
+
def uri
|
15
|
+
parsed_host.merge URI::Generic.build(:path => path, :query => { :token => token }.to_query)
|
16
|
+
end
|
17
|
+
|
18
|
+
def import!
|
19
|
+
import_json import_data
|
20
|
+
end
|
21
|
+
|
22
|
+
def import_data
|
23
|
+
Net::HTTP.get(uri)
|
24
|
+
rescue
|
25
|
+
raise PufferPages::ImportFailed.new("Could not connect to `#{name}` (#{uri})")
|
26
|
+
end
|
27
|
+
|
28
|
+
def import_json json
|
29
|
+
data = json.is_a?(String) ? ActiveSupport::JSON.decode(json) : json
|
30
|
+
ActiveRecord::Base.transaction do
|
31
|
+
%w(layouts snippets pages).each do |table|
|
32
|
+
klass = "puffer_pages/#{table}".classify.constantize
|
33
|
+
klass.import_json data[table]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.export_json
|
39
|
+
%w(layouts snippets pages).each_with_object({}) do |table, result|
|
40
|
+
klass = "puffer_pages/#{table}".classify.constantize
|
41
|
+
result[table] = klass.export_json
|
42
|
+
end.as_json.to_json
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def parsed_host
|
48
|
+
URI.parse([scheme, real_host].join('://'))
|
49
|
+
end
|
50
|
+
|
51
|
+
def real_host
|
52
|
+
host.gsub(/\Ahttps?\:\/\//, '')
|
53
|
+
end
|
54
|
+
|
55
|
+
def scheme
|
56
|
+
match = host.match(/\A(https?)\:\/\//)
|
57
|
+
match ? match[1] : 'http'
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,221 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
class PufferPages::Backends::Page < ActiveRecord::Base
|
3
|
+
include ActiveUUID::UUID
|
4
|
+
include PufferPages::Backends::Mixins::Renderable
|
5
|
+
include PufferPages::Backends::Mixins::Importable
|
6
|
+
include PufferPages::Backends::Mixins::Localable
|
7
|
+
self.abstract_class = true
|
8
|
+
self.table_name = :pages
|
9
|
+
|
10
|
+
attr_protected :location
|
11
|
+
|
12
|
+
def self.inherited base
|
13
|
+
base.acts_as_nested_set
|
14
|
+
super
|
15
|
+
end
|
16
|
+
|
17
|
+
scope :published, where(:status => 'published')
|
18
|
+
|
19
|
+
def self.statuses
|
20
|
+
%w(draft hidden published)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.controller_scope scope
|
24
|
+
where scope
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.normalize_path path
|
28
|
+
path.to_s.split('/').delete_if(&:blank?).join('/').presence
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.find_page location
|
32
|
+
location = normalize_path(location)
|
33
|
+
page = PufferPages.single_section_page_path ?
|
34
|
+
find_by_slug(location) : find_by_location(location)
|
35
|
+
raise ActiveRecord::RecordNotFound unless page
|
36
|
+
raise PufferPages::DraftPage.new("PufferPages can`t show page `#{page.location}` because it is draft") if page.draft?
|
37
|
+
page
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.find_view_page location, options = {}
|
41
|
+
location = normalize_path(location)
|
42
|
+
depth = location.to_s.count('/').next
|
43
|
+
formats = options[:formats].presence || [:html]
|
44
|
+
|
45
|
+
page = if location.blank?
|
46
|
+
roots.first
|
47
|
+
else
|
48
|
+
formats.inject(nil) do |page, format|
|
49
|
+
page ||= begin
|
50
|
+
loc = if format == :html
|
51
|
+
where(['? like location', location])
|
52
|
+
else
|
53
|
+
where(['? like location', [location, format].join('.')]).where(['not ? like location', [location, format, ''].join('.')])
|
54
|
+
end
|
55
|
+
loc.where(['status not in (?)', 'draft']).where(:"#{depth_column_name}" => depth).order('lft desc').first
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
raise PufferPages::LayoutMissed.new(
|
61
|
+
"PufferPages can`t render page for `#{location}` with formats `#{formats.join('`, `')}` because layout page missed or draft"
|
62
|
+
) unless page
|
63
|
+
page
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.export_json
|
67
|
+
includes(:page_parts).order(:lft).as_json(
|
68
|
+
include: :page_parts, except: [:lft, :rgt, :depth, :location]
|
69
|
+
)
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.import_destroy
|
73
|
+
roots.destroy_all
|
74
|
+
end
|
75
|
+
|
76
|
+
has_many_page_parts_options = {
|
77
|
+
order: "name = '#{PufferPages.primary_page_part_name}' desc, name",
|
78
|
+
dependent: :destroy,
|
79
|
+
class_name: '::PufferPages::PagePart',
|
80
|
+
validate: true,
|
81
|
+
inverse_of: :page
|
82
|
+
}
|
83
|
+
has_many_page_parts_options.merge!(include: :translations) if PufferPages.localize
|
84
|
+
|
85
|
+
has_many :page_parts, has_many_page_parts_options
|
86
|
+
|
87
|
+
accepts_nested_attributes_for :page_parts, :allow_destroy => true
|
88
|
+
belongs_to :layout, :primary_key => :name, :foreign_key => :layout_name, :class_name => 'PufferPages::Layout'
|
89
|
+
|
90
|
+
validates_presence_of :name
|
91
|
+
validates_uniqueness_of :slug,
|
92
|
+
:scope => (:parent_id unless PufferPages.single_section_page_path), :allow_nil => true
|
93
|
+
validates_inclusion_of :status, :in => statuses
|
94
|
+
validates_format_of :slug,
|
95
|
+
:with => /\A\s*\Z/, :message => :root_slug_format, :if => :root?
|
96
|
+
validates_format_of :slug,
|
97
|
+
:with => /\A([^\/]+)\Z/, :message => :slug_format, :unless => :root?
|
98
|
+
validate do |page|
|
99
|
+
page.errors.add(:layout_name, :blank) if page.inherited_layout_name.nil?
|
100
|
+
end
|
101
|
+
|
102
|
+
before_validation :defaultize_attributes
|
103
|
+
def defaultize_attributes
|
104
|
+
self.status ||= 'draft'
|
105
|
+
self.slug = slug.presence
|
106
|
+
self.location = [parent.try(:location), slug].compact.join('/').presence
|
107
|
+
end
|
108
|
+
|
109
|
+
def location
|
110
|
+
read_attribute(:location) || [parent.try(:location), slug].compact.join('/').presence
|
111
|
+
end
|
112
|
+
|
113
|
+
before_update :update_locations, :if => :location_changed?
|
114
|
+
def update_locations
|
115
|
+
self.class.update_all "location = replace(location, '#{location_was}', '#{location}')", ["location like ?", location_was + '%']
|
116
|
+
end
|
117
|
+
|
118
|
+
statuses.each do |status_name|
|
119
|
+
define_method "#{status_name}?" do status == status_name end
|
120
|
+
end
|
121
|
+
|
122
|
+
def status
|
123
|
+
ActiveSupport::StringInquirer.new(read_attribute(:status)) if status?
|
124
|
+
end
|
125
|
+
|
126
|
+
def segments
|
127
|
+
location.to_s.split ?/
|
128
|
+
end
|
129
|
+
|
130
|
+
def to_location
|
131
|
+
PufferPages.single_section_page_path ? slug : location
|
132
|
+
end
|
133
|
+
|
134
|
+
def format
|
135
|
+
File.extname(slug)[1..-1].to_s.to_sym.presence || :html
|
136
|
+
end
|
137
|
+
|
138
|
+
def ancestors_page_parts
|
139
|
+
self_and_ancestors_page_parts.where('pages.id != ?', id)
|
140
|
+
end
|
141
|
+
|
142
|
+
def self_and_ancestors_page_parts
|
143
|
+
PufferPages::PagePart
|
144
|
+
.where("#{q_left} <= ? AND #{q_right} >= ?", left, right)
|
145
|
+
.joins(:page).order('name, pages.lft desc')
|
146
|
+
end
|
147
|
+
|
148
|
+
def inherited_page_parts
|
149
|
+
@inherited_page_parts ||= self_and_ancestors_page_parts.group_by(&:name).map { |(_, group)| group.first }
|
150
|
+
end
|
151
|
+
|
152
|
+
def inherited_page_part name
|
153
|
+
inherited_page_parts.detect { |part| part.name == name }
|
154
|
+
end
|
155
|
+
|
156
|
+
def render *args
|
157
|
+
source, context = normalize_render_options *args
|
158
|
+
context = merge_context context, additional_render_options
|
159
|
+
source ||= inherited_layout
|
160
|
+
|
161
|
+
contextualize page_translations: page_translations do
|
162
|
+
if source
|
163
|
+
if source.respond_to?(:render)
|
164
|
+
instrument_render! context do
|
165
|
+
source.render context
|
166
|
+
end
|
167
|
+
else
|
168
|
+
render_template source, context
|
169
|
+
end
|
170
|
+
else
|
171
|
+
instrument_render! context do
|
172
|
+
inherited_page_parts.map do |part|
|
173
|
+
result = part.render context
|
174
|
+
part.main? ? result : "<% content_for :'#{part.name}' do %>#{result}<% end %>"
|
175
|
+
end.join
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
def locales_translations; locales; end
|
182
|
+
def locales_translations=(value); self.locales = value; end
|
183
|
+
|
184
|
+
def page_translations
|
185
|
+
self_and_ancestors.each_with_object({}) do |page, result|
|
186
|
+
result.deep_merge! page.locales.translations
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
def additional_render_options
|
191
|
+
{ registers: { page: self }, drops: { page: self, self: self } }
|
192
|
+
end
|
193
|
+
|
194
|
+
def inherited_layout_page
|
195
|
+
@inherited_layout_page ||= layout_name? ? self : parent.try(:inherited_layout_page)
|
196
|
+
end
|
197
|
+
|
198
|
+
def inherited_layout_name
|
199
|
+
@inherited_layout_name ||= inherited_layout_page.try(:layout_name)
|
200
|
+
end
|
201
|
+
|
202
|
+
def current_layout
|
203
|
+
@current_layout ||= inherited_layout_page.try(:layout)
|
204
|
+
end
|
205
|
+
|
206
|
+
def inherited_layout
|
207
|
+
@inherited_layout ||= PufferPages::Layout.find_layout(current_layout.name) if current_layout
|
208
|
+
end
|
209
|
+
|
210
|
+
def layout_for_render
|
211
|
+
"layouts/#{inherited_layout_name}" unless inherited_layout
|
212
|
+
end
|
213
|
+
|
214
|
+
def content_type
|
215
|
+
Rack::Mime.mime_type(File.extname(slug.to_s), 'text/html')
|
216
|
+
end
|
217
|
+
|
218
|
+
def to_liquid
|
219
|
+
@to_liquid ||= ::PufferPages::Liquid::PageDrop.new(self)
|
220
|
+
end
|
221
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
class PufferPages::Backends::PagePart < ActiveRecord::Base
|
2
|
+
include ActiveUUID::UUID
|
3
|
+
include PufferPages::Backends::Mixins::Renderable
|
4
|
+
include PufferPages::Backends::Mixins::Importable
|
5
|
+
include PufferPages::Backends::Mixins::Translatable
|
6
|
+
self.abstract_class = true
|
7
|
+
self.table_name = :page_parts
|
8
|
+
|
9
|
+
attr_protected
|
10
|
+
|
11
|
+
default_scope ->{ includes :translations } if PufferPages.localize
|
12
|
+
|
13
|
+
validates_presence_of :name
|
14
|
+
validates_uniqueness_of :name, :scope => :page_id
|
15
|
+
|
16
|
+
belongs_to :page, :class_name => '::PufferPages::Page', :inverse_of => :page_parts
|
17
|
+
|
18
|
+
before_validation :defaultize_attributes
|
19
|
+
def defaultize_attributes
|
20
|
+
self.handler ||= 'html'
|
21
|
+
end
|
22
|
+
|
23
|
+
def main?
|
24
|
+
name == PufferPages.primary_page_part_name
|
25
|
+
end
|
26
|
+
|
27
|
+
def parent
|
28
|
+
ancestors.first
|
29
|
+
end
|
30
|
+
|
31
|
+
def ancestors
|
32
|
+
page.ancestors_page_parts.where(name: name)
|
33
|
+
end
|
34
|
+
|
35
|
+
def self_and_ancestors
|
36
|
+
page.self_and_ancestors_page_parts.where(name: name)
|
37
|
+
end
|
38
|
+
|
39
|
+
def render *args
|
40
|
+
_, context = normalize_render_options *args
|
41
|
+
render_template body, context, additional_render_options
|
42
|
+
end
|
43
|
+
|
44
|
+
def handle *args
|
45
|
+
_, context = normalize_render_options *args
|
46
|
+
PufferPages::Handlers.process handler || 'html', self, context
|
47
|
+
end
|
48
|
+
|
49
|
+
def additional_render_options
|
50
|
+
{ environment: { processed: self } }
|
51
|
+
end
|
52
|
+
|
53
|
+
def i18n_scope
|
54
|
+
i18n_scope_for page.segments, :page_parts, name
|
55
|
+
end
|
56
|
+
|
57
|
+
def i18n_defaults
|
58
|
+
page.segments.inject([]) do |memo, element|
|
59
|
+
memo.push (memo.last || []).dup.push(element)
|
60
|
+
end.unshift([]).inject([]) do |memo, segments|
|
61
|
+
memo.unshift i18n_scope_for(segments)
|
62
|
+
memo.unshift i18n_scope_for(segments, :page_parts, name)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def i18n_scope_for *segments
|
69
|
+
[:pages, *segments.flatten.map(&:to_sym)]
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
class PufferPages::Backends::Snippet < ActiveRecord::Base
|
2
|
+
include ActiveUUID::UUID
|
3
|
+
include PufferPages::Backends::Mixins::Renderable
|
4
|
+
include PufferPages::Backends::Mixins::Importable
|
5
|
+
include PufferPages::Backends::Mixins::Translatable
|
6
|
+
self.abstract_class = true
|
7
|
+
self.table_name = :snippets
|
8
|
+
|
9
|
+
attr_protected
|
10
|
+
|
11
|
+
validates_presence_of :name
|
12
|
+
validates_uniqueness_of :name
|
13
|
+
|
14
|
+
def self.find_snippet(name)
|
15
|
+
where(:name => name).first
|
16
|
+
end
|
17
|
+
|
18
|
+
def render *args
|
19
|
+
_, context = normalize_render_options *args
|
20
|
+
render_template body, context, additional_render_options
|
21
|
+
end
|
22
|
+
|
23
|
+
def additional_render_options
|
24
|
+
{ environment: { processed: self } }
|
25
|
+
end
|
26
|
+
|
27
|
+
def i18n_scope
|
28
|
+
[:snippets, name.to_sym]
|
29
|
+
end
|
30
|
+
|
31
|
+
def i18n_defaults
|
32
|
+
[]
|
33
|
+
end
|
34
|
+
end
|
data/lib/puffer_pages/engine.rb
CHANGED
@@ -1,9 +1,20 @@
|
|
1
1
|
module PufferPages
|
2
2
|
class Engine < Rails::Engine
|
3
|
+
engine_name :puffer_pages
|
4
|
+
|
3
5
|
config.autoload_paths << File.join(root, 'lib')
|
4
6
|
|
5
|
-
|
6
|
-
|
7
|
+
config.puffer_pages = ActiveSupport::OrderedOptions.new
|
8
|
+
config.puffer_pages.raise_errors = false
|
9
|
+
|
10
|
+
config.after_initialize do
|
11
|
+
if PufferPages.install_i18n_backend
|
12
|
+
I18n.backend = I18n::Backend::Chain.new(PufferPages.i18n_backend, I18n.backend)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
ActiveSupport.on_load(:action_view) do
|
17
|
+
ActionView::Base.send :include, PufferPages::Helpers
|
7
18
|
end
|
8
19
|
end
|
9
20
|
end
|