pages_core 3.4.3 → 3.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +81 -15
- data/Rakefile +1 -1
- data/app/assets/javascripts/pages/admin.es6.jsx +19 -0
- data/app/assets/javascripts/pages/admin/components.es6.jsx +1 -0
- data/app/assets/javascripts/pages/admin/components/page_tree.es6.jsx +330 -0
- data/app/assets/javascripts/pages/admin/components/page_tree_actions.es6.jsx +8 -0
- data/app/assets/javascripts/pages/admin/components/page_tree_node.es6.jsx +374 -0
- data/app/assets/javascripts/pages/admin/components/page_tree_store.es6.jsx +161 -0
- data/app/assets/javascripts/pages/admin/features/content_tabs.es6.jsx +63 -0
- data/app/assets/javascripts/pages/admin/features/edit_page.es6.jsx +141 -0
- data/app/assets/javascripts/pages/admin/features/editable_image.es6.jsx +145 -0
- data/app/assets/javascripts/pages/admin/features/modal.es6.jsx +90 -0
- data/app/assets/javascripts/pages/admin/features/page_images.es6.jsx +338 -0
- data/app/assets/javascripts/pages/admin/features/rich_text.es6.jsx +124 -0
- data/app/assets/javascripts/pages/admin/features/tag_editor.es6.jsx +160 -0
- data/app/assets/javascripts/pages/admin/lib/ajax_extensions.es6.jsx +21 -0
- data/app/assets/javascripts/pages/admin/lib/center_on_screen.es6.jsx +22 -0
- data/app/assets/javascripts/pages/admin/lib/tree.es6.jsx +294 -0
- data/app/assets/javascripts/pages/login_form.es6.jsx +21 -0
- data/app/assets/stylesheets/pages/admin.scss +148 -0
- data/app/assets/stylesheets/pages/admin/components/buttons.scss +5 -0
- data/app/assets/stylesheets/pages/admin/{editable_image.css.erb → components/editable_image.scss} +7 -8
- data/app/assets/stylesheets/pages/admin/components/forms.scss +71 -0
- data/app/assets/stylesheets/pages/admin/components/header.scss +169 -0
- data/app/assets/stylesheets/pages/admin/{images.css.scss.erb → components/images.scss} +6 -11
- data/app/assets/stylesheets/pages/admin/components/layout.scss +44 -0
- data/app/assets/stylesheets/pages/admin/components/links.scss +43 -0
- data/app/assets/stylesheets/pages/admin/components/list_table.scss +58 -0
- data/app/assets/stylesheets/pages/admin/{login.css.scss.erb → components/login.scss} +1 -1
- data/app/assets/stylesheets/pages/admin/{modal.css.erb → components/modal.scss} +3 -2
- data/app/assets/stylesheets/pages/admin/components/page_tree.scss +173 -0
- data/app/assets/stylesheets/pages/admin/{pagination.css.scss → components/pagination.scss} +13 -4
- data/app/assets/stylesheets/pages/admin/components/sidebar.scss +25 -0
- data/app/assets/stylesheets/pages/admin/{tag_editor.css.scss.erb → components/tag_editor.scss} +6 -0
- data/app/assets/stylesheets/pages/admin/components/textarea.scss +76 -0
- data/app/assets/stylesheets/pages/admin/controllers/pages.scss +196 -0
- data/app/assets/stylesheets/pages/admin/controllers/{users.css.erb → users.scss} +0 -0
- data/app/assets/stylesheets/pages/admin/mixins/breakpoints.scss +21 -0
- data/app/assets/stylesheets/pages/admin/mixins/clearfix.scss +7 -0
- data/app/assets/stylesheets/pages/admin/mixins/gradients.scss +7 -0
- data/app/assets/stylesheets/pages/admin/{print.css.erb → print.scss} +0 -0
- data/app/assets/stylesheets/pages/admin/vars.scss +8 -0
- data/app/controllers/admin/invites_controller.rb +10 -6
- data/app/controllers/admin/page_files_controller.rb +6 -8
- data/app/controllers/admin/page_images_controller.rb +14 -19
- data/app/controllers/admin/pages_controller.rb +44 -97
- data/app/controllers/admin/password_resets_controller.rb +7 -2
- data/app/controllers/concerns/pages_core/add_comments_controller.rb +67 -0
- data/app/controllers/concerns/pages_core/admin/news_page_controller.rb +58 -0
- data/app/controllers/concerns/pages_core/authentication.rb +1 -1
- data/app/controllers/concerns/pages_core/exception_handler.rb +44 -21
- data/app/controllers/concerns/pages_core/policies_helper.rb +10 -6
- data/app/controllers/concerns/pages_core/preview_pages_controller.rb +43 -0
- data/app/controllers/concerns/pages_core/process_titler.rb +2 -2
- data/app/controllers/concerns/pages_core/rss_controller.rb +25 -0
- data/app/controllers/concerns/pages_core/search_pages_controller.rb +40 -0
- data/app/controllers/errors_controller.rb +14 -2
- data/app/controllers/pages_core/admin_controller.rb +7 -5
- data/app/controllers/pages_core/frontend/page_files_controller.rb +5 -7
- data/app/controllers/pages_core/frontend/pages_controller.rb +41 -219
- data/app/controllers/pages_core/frontend_controller.rb +8 -2
- data/app/controllers/pages_core/sitemaps_controller.rb +5 -4
- data/app/formatters/pages_core/html_formatter.rb +33 -23
- data/app/helpers/admin/menu_helper.rb +12 -9
- data/app/helpers/admin/pages_helper.rb +40 -28
- data/app/helpers/pages_core/admin/admin_helper.rb +58 -56
- data/app/helpers/pages_core/admin/labelled_field_helper.rb +6 -7
- data/app/helpers/pages_core/admin/tag_editor_helper.rb +11 -9
- data/app/helpers/pages_core/application_helper.rb +13 -26
- data/app/helpers/pages_core/form_builder.rb +71 -134
- data/app/helpers/pages_core/head_tags_helper.rb +26 -168
- data/app/helpers/pages_core/images_helper.rb +3 -3
- data/app/helpers/pages_core/meta_tags_helper.rb +96 -0
- data/app/helpers/pages_core/open_graph_tags_helper.rb +51 -0
- data/app/helpers/pages_core/page_path_helper.rb +40 -0
- data/app/mailers/admin_mailer.rb +14 -14
- data/app/models/autopublisher.rb +2 -2
- data/app/models/category.rb +8 -8
- data/app/models/concerns/pages_core/has_roles.rb +2 -2
- data/app/models/concerns/pages_core/humanizable_param.rb +5 -5
- data/app/models/concerns/pages_core/page_model/autopublishable.rb +25 -0
- data/app/models/concerns/pages_core/page_model/commentable.rb +29 -0
- data/app/models/concerns/pages_core/page_model/images.rb +50 -0
- data/app/models/concerns/pages_core/page_model/localizable.rb +29 -0
- data/app/models/concerns/pages_core/page_model/pathable.rb +115 -0
- data/app/models/concerns/pages_core/page_model/redirectable.rb +36 -0
- data/app/models/concerns/pages_core/page_model/searchable.rb +41 -0
- data/app/models/concerns/pages_core/page_model/sortable.rb +54 -0
- data/app/models/concerns/pages_core/page_model/status.rb +50 -0
- data/app/models/concerns/pages_core/page_model/templateable.rb +82 -0
- data/app/models/concerns/pages_core/page_model/tree.rb +108 -0
- data/app/models/page.rb +30 -212
- data/app/models/page_builder.rb +4 -6
- data/app/models/page_category.rb +7 -0
- data/app/models/page_comment.rb +1 -1
- data/app/models/page_file.rb +4 -6
- data/app/models/page_image.rb +6 -7
- data/app/models/page_path.rb +46 -0
- data/app/models/password_reset_token.rb +5 -5
- data/app/models/role.rb +1 -1
- data/app/models/tag.rb +14 -16
- data/app/models/tagging.rb +2 -1
- data/app/models/user.rb +6 -7
- data/app/policies/page_policy.rb +8 -4
- data/app/policies/user_policy.rb +1 -1
- data/app/serializers/page_tree_serializer.rb +15 -0
- data/app/views/admin/invites/new.html.erb +2 -1
- data/app/views/admin/invites/show.html.erb +3 -4
- data/app/views/admin/pages/_edit_comments.html.erb +22 -6
- data/app/views/admin/pages/_edit_content.html.erb +4 -2
- data/app/views/admin/pages/_edit_images.html.erb +86 -75
- data/app/views/admin/pages/_edit_metadata.html.erb +22 -0
- data/app/views/admin/pages/_edit_options.html.erb +23 -15
- data/app/views/admin/pages/_pagelisting.html.erb +6 -6
- data/app/views/admin/pages/edit.html.erb +11 -6
- data/app/views/admin/pages/index.html.erb +12 -53
- data/app/views/admin/pages/new.html.erb +3 -3
- data/app/views/admin/pages/news.html.erb +1 -1
- data/app/views/admin_mailer/invite.text.erb +1 -1
- data/app/views/admin_mailer/password_reset.text.erb +1 -1
- data/app/views/errors/422.html.erb +7 -0
- data/app/views/errors/500_critical.html.erb +1 -1
- data/app/views/layouts/admin.html.erb +36 -32
- data/app/views/layouts/admin/_header.html.erb +2 -2
- data/config/locales/en.yml +38 -1
- data/config/routes.rb +40 -23
- data/db/migrate/20111219033112_create_pages_tables.rb +25 -29
- data/db/migrate/20121010055412_drop_removed_tables.rb +3 -3
- data/db/migrate/20130823133208_update_page_redirect_to.rb +0 -13
- data/db/migrate/20140203183900_create_roles.rb +5 -2
- data/db/migrate/20140920231700_convert_images_to_dis.rb +4 -2
- data/db/migrate/20150401131300_localize_images.rb +7 -8
- data/db/migrate/20151002174800_create_page_paths.rb +10 -0
- data/db/migrate/20151021103400_drop_binaries_table.rb +7 -0
- data/db/migrate/20151204151000_remove_page_content_order.rb +5 -0
- data/db/migrate/20160330220900_rename_pages_categories.rb +6 -0
- data/db/migrate/20160405202700_change_localization_limit.rb +9 -0
- data/lib/pages_core.rb +22 -27
- data/lib/pages_core/admin_menu_item.rb +16 -3
- data/lib/pages_core/archive_finder.rb +40 -13
- data/lib/pages_core/cache_sweeper.rb +72 -45
- data/lib/pages_core/configuration.rb +2 -2
- data/lib/pages_core/configuration/base.rb +4 -8
- data/lib/pages_core/configuration/pages.rb +6 -3
- data/lib/pages_core/engine.rb +23 -1
- data/lib/pages_core/extensions.rb +2 -2
- data/lib/pages_core/file_embedder.rb +40 -0
- data/lib/pages_core/page_path_constraint.rb +23 -0
- data/lib/pages_core/pages_plugin.rb +11 -0
- data/lib/pages_core/paginates.rb +3 -3
- data/lib/pages_core/plugin.rb +14 -8
- data/lib/pages_core/templates.rb +6 -6
- data/lib/pages_core/templates/block_configuration.rb +1 -1
- data/lib/pages_core/templates/configuration.rb +23 -24
- data/lib/pages_core/templates/configuration_handler.rb +1 -1
- data/lib/pages_core/templates/configuration_proxy.rb +7 -11
- data/lib/pages_core/templates/template_configuration.rb +55 -61
- data/lib/pages_core/version.rb +1 -1
- data/lib/rails/generators/pages_core/install/install_generator.rb +22 -48
- data/lib/rails/generators/pages_core/install/templates/page_templates_initializer.rb +1 -1
- data/lib/rails/generators/pages_core/install/templates/pages_initializer.rb +6 -3
- data/lib/rails/generators/pages_core/rspec/rspec_generator.rb +4 -1
- data/lib/rails/generators/pages_core/rspec/templates/factories.rb +1 -1
- data/lib/tasks/pages.rake +4 -4
- data/lib/tasks/pages/page_paths.rake +12 -0
- data/template.rb +2 -2
- data/vendor/assets/javascripts/reflux.min.js +1 -0
- metadata +173 -85
- data/app/assets/images/pages/admin/description-bg.gif +0 -0
- data/app/assets/images/pages/admin/drag-handle.gif +0 -0
- data/app/assets/images/pages/admin/flash-error-bg.gif +0 -0
- data/app/assets/images/pages/admin/formelement-bg.gif +0 -0
- data/app/assets/images/pages/admin/header-tab-current-bg.gif +0 -0
- data/app/assets/images/pages/admin/list-table-td-bg.gif +0 -0
- data/app/assets/images/pages/admin/sidebar-bg.gif +0 -0
- data/app/assets/images/pages/admin/textarea_controls.gif +0 -0
- data/app/assets/javascripts/pages/admin.js.coffee +0 -54
- data/app/assets/javascripts/pages/admin/controllers/base.js.coffee +0 -4
- data/app/assets/javascripts/pages/admin/controllers/pages_controller.js.coffee +0 -139
- data/app/assets/javascripts/pages/admin/controllers/users_controller.js.coffee +0 -9
- data/app/assets/javascripts/pages/admin/features/content_tabs.js.coffee +0 -47
- data/app/assets/javascripts/pages/admin/features/editable_image.js.coffee.erb +0 -122
- data/app/assets/javascripts/pages/admin/features/modal.js.coffee +0 -66
- data/app/assets/javascripts/pages/admin/features/page_images.js +0 -329
- data/app/assets/javascripts/pages/admin/features/rich_text.js.coffee +0 -40
- data/app/assets/javascripts/pages/admin/features/tag_editor.js +0 -159
- data/app/assets/javascripts/pages/admin/lib/ajax_extensions.js.coffee +0 -17
- data/app/assets/javascripts/pages/admin/lib/center_on_screen.js.coffee +0 -21
- data/app/assets/javascripts/pages/admin/lib/jrichtextarea.js +0 -57
- data/app/assets/javascripts/pages/login_form.js.coffee +0 -17
- data/app/assets/stylesheets/pages/admin.css.erb +0 -404
- data/app/assets/stylesheets/pages/admin/buttons.css.erb +0 -5
- data/app/assets/stylesheets/pages/admin/controllers/artists.css.erb +0 -94
- data/app/assets/stylesheets/pages/admin/controllers/files.css.erb +0 -58
- data/app/assets/stylesheets/pages/admin/controllers/pages.css.scss.erb +0 -178
- data/app/assets/stylesheets/pages/admin/forms.css.scss.erb +0 -73
- data/app/assets/stylesheets/pages/admin/header.css.erb +0 -129
- data/app/assets/stylesheets/pages/admin/links.css.erb +0 -34
- data/app/assets/stylesheets/pages/admin/list_table.css.erb +0 -56
- data/app/assets/stylesheets/pages/admin/sidebar.css.erb +0 -39
- data/app/assets/stylesheets/pages/admin/sortable_images.css.erb +0 -18
- data/app/assets/stylesheets/pages/admin/textarea.css.erb +0 -55
- data/app/models/concerns/pages_core/page_tree.rb +0 -85
- data/app/models/concerns/pages_core/searchable_page.rb +0 -33
- data/app/models/concerns/pages_core/templateable.rb +0 -85
- data/app/models/localization.rb +0 -27
- data/db/migrate/20140515130100_remove_sphinx_deltas.rb +0 -15
- data/lib/pages_core/localizable.rb +0 -49
- data/lib/pages_core/localizable/active_record_extension.rb +0 -41
- data/lib/pages_core/localizable/class_methods.rb +0 -51
- data/lib/pages_core/localizable/configuration.rb +0 -50
- data/lib/pages_core/localizable/instance_methods.rb +0 -130
- data/lib/pages_core/localizable/localizer.rb +0 -72
- data/lib/pages_core/localizable/scope_extension.rb +0 -22
@@ -1,49 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
load "pages_core/localizable/active_record_extension.rb"
|
4
|
-
load "pages_core/localizable/class_methods.rb"
|
5
|
-
load "pages_core/localizable/configuration.rb"
|
6
|
-
load "pages_core/localizable/instance_methods.rb"
|
7
|
-
load "pages_core/localizable/localizer.rb"
|
8
|
-
load "pages_core/localizable/scope_extension.rb"
|
9
|
-
|
10
|
-
module PagesCore
|
11
|
-
# = Localizable
|
12
|
-
#
|
13
|
-
# Localizable allows any model to have localized attributes.
|
14
|
-
#
|
15
|
-
# == Configuring the model
|
16
|
-
#
|
17
|
-
# class Page < ActiveRecord::Base
|
18
|
-
# localizable do
|
19
|
-
# attribute :name
|
20
|
-
# attribute :body
|
21
|
-
# end
|
22
|
-
# end
|
23
|
-
#
|
24
|
-
# == Usage
|
25
|
-
#
|
26
|
-
# page = Page.create(name: 'Hello', locale: 'en')
|
27
|
-
# page.name? # => true
|
28
|
-
# page.name.to_s # => 'Hello'
|
29
|
-
#
|
30
|
-
# The localized attributes always return an instance of Localization.
|
31
|
-
#
|
32
|
-
# To get a localized version of a page, call .localize on it:
|
33
|
-
#
|
34
|
-
# page = Page.first.localize('en')
|
35
|
-
#
|
36
|
-
# .localize also takes a block argument:
|
37
|
-
#
|
38
|
-
# page.localize('nb') do |p|
|
39
|
-
# p.locale # => 'nb'
|
40
|
-
# end
|
41
|
-
# page.locale # => 'en'
|
42
|
-
#
|
43
|
-
# Multiple locales can be updated at the same time:
|
44
|
-
#
|
45
|
-
# page.name = {'en' => 'Hello', 'nb' => 'Hallo'}
|
46
|
-
#
|
47
|
-
module Localizable
|
48
|
-
end
|
49
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module PagesCore
|
4
|
-
module Localizable
|
5
|
-
# = Localizable::ActiveRecordExtension
|
6
|
-
#
|
7
|
-
# Extends ActiveRecord::Base with the localizable setup method.
|
8
|
-
#
|
9
|
-
module ActiveRecordExtension
|
10
|
-
# Extends the model with Localizable features.
|
11
|
-
# It takes an optional block as argument, which yields an instance of
|
12
|
-
# Localizable::Configuration.
|
13
|
-
#
|
14
|
-
# Example:
|
15
|
-
#
|
16
|
-
# class Page < ActiveRecord::Base
|
17
|
-
# localizable do
|
18
|
-
# attribute :name
|
19
|
-
# attribute :body
|
20
|
-
# end
|
21
|
-
# end
|
22
|
-
#
|
23
|
-
def localizable(&block)
|
24
|
-
unless self.is_a?(Localizable::ClassMethods)
|
25
|
-
send :extend, Localizable::ClassMethods
|
26
|
-
send :include, Localizable::InstanceMethods
|
27
|
-
has_many(
|
28
|
-
:localizations,
|
29
|
-
as: :localizable,
|
30
|
-
dependent: :destroy,
|
31
|
-
autosave: true
|
32
|
-
)
|
33
|
-
before_save :cleanup_localizations!
|
34
|
-
end
|
35
|
-
localizable_configuration.instance_eval(&block) if block_given?
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
ActiveRecord::Base.send(:extend, PagesCore::Localizable::ActiveRecordExtension)
|
@@ -1,51 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module PagesCore
|
4
|
-
module Localizable
|
5
|
-
# = Localizable::ClassMethods
|
6
|
-
#
|
7
|
-
# Class methods for all Localizable models.
|
8
|
-
#
|
9
|
-
module ClassMethods
|
10
|
-
# Returns a scope where all records will be set to the given locale.
|
11
|
-
#
|
12
|
-
def in_locale(locale)
|
13
|
-
all
|
14
|
-
.extending(Localizable::ScopeExtension)
|
15
|
-
.localize(locale)
|
16
|
-
.includes(:localizations)
|
17
|
-
end
|
18
|
-
|
19
|
-
# Returns a scope with only records matching the given locale.
|
20
|
-
#
|
21
|
-
# Page.localized('en').first.locale # => 'en'
|
22
|
-
#
|
23
|
-
def localized(locale)
|
24
|
-
in_locale(locale)
|
25
|
-
.where("localizations.locale = ?", locale)
|
26
|
-
.references(:localizations)
|
27
|
-
end
|
28
|
-
|
29
|
-
def localized_attributes
|
30
|
-
localizable_configuration.attributes.keys
|
31
|
-
end
|
32
|
-
|
33
|
-
# Accessor for the configuration.
|
34
|
-
def localizable_configuration
|
35
|
-
@localizable_configuration ||= inherited_localizable_configuration
|
36
|
-
end
|
37
|
-
|
38
|
-
private
|
39
|
-
|
40
|
-
def inherited_localizable_configuration
|
41
|
-
if superclass.respond_to?(:localizable_configuration)
|
42
|
-
Localizable::Configuration.new(
|
43
|
-
superclass.localizable_configuration.attributes.dup
|
44
|
-
)
|
45
|
-
else
|
46
|
-
Localizable::Configuration.new
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
@@ -1,50 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module PagesCore
|
4
|
-
module Localizable
|
5
|
-
class Configuration
|
6
|
-
def initialize(attributes = nil)
|
7
|
-
@attributes = attributes
|
8
|
-
end
|
9
|
-
|
10
|
-
def attribute(attribute_name, options = {})
|
11
|
-
attribute_table[attribute_name.to_sym] = options
|
12
|
-
end
|
13
|
-
|
14
|
-
def attributes
|
15
|
-
attribute_table.merge(dictionary_attributes)
|
16
|
-
end
|
17
|
-
|
18
|
-
def dictionary(dict)
|
19
|
-
dictionaries << dict
|
20
|
-
end
|
21
|
-
|
22
|
-
def attribute?(attribute)
|
23
|
-
attributes.keys.include?(attribute)
|
24
|
-
end
|
25
|
-
|
26
|
-
private
|
27
|
-
|
28
|
-
def dictionaries
|
29
|
-
@dictionaries ||= []
|
30
|
-
end
|
31
|
-
|
32
|
-
def dictionary_attributes
|
33
|
-
dictionaries.map(&:call).inject({}) do |attrs, list|
|
34
|
-
attrs.merge(hashify(list))
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def hashify(list)
|
39
|
-
return list if list.is_a?(Hash)
|
40
|
-
list.each_with_object({}) do |e, a|
|
41
|
-
a[e] = {}
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def attribute_table
|
46
|
-
@attributes ||= {}
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
@@ -1,130 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module PagesCore
|
4
|
-
module Localizable
|
5
|
-
# = Localizable::InstanceMethods
|
6
|
-
#
|
7
|
-
# This is the public API for all localizable models.
|
8
|
-
# See Localizable for usage examples.
|
9
|
-
#
|
10
|
-
module InstanceMethods
|
11
|
-
# Returns all locales saved for this page.
|
12
|
-
#
|
13
|
-
def locales
|
14
|
-
localizer.locales
|
15
|
-
end
|
16
|
-
|
17
|
-
# Getter for locale
|
18
|
-
#
|
19
|
-
# page.locale # => 'en'
|
20
|
-
#
|
21
|
-
def locale
|
22
|
-
localizer.locale
|
23
|
-
end
|
24
|
-
|
25
|
-
# Setter for locale
|
26
|
-
#
|
27
|
-
# page.locale = 'no' # => 'no'
|
28
|
-
#
|
29
|
-
def locale=(locale)
|
30
|
-
@localizer = Localizer.new(self)
|
31
|
-
localizer.locale = locale
|
32
|
-
end
|
33
|
-
|
34
|
-
# Returns true if this page has a locale set
|
35
|
-
#
|
36
|
-
def locale?
|
37
|
-
localizer.locale?
|
38
|
-
end
|
39
|
-
|
40
|
-
# Returns a copy of the model with a different locale.
|
41
|
-
#
|
42
|
-
# localized = page.localize('en')
|
43
|
-
#
|
44
|
-
# localize also takes a block as an argument, which returns the
|
45
|
-
# result of the block.
|
46
|
-
#
|
47
|
-
# page.localize('nb') do |p|
|
48
|
-
# p.locale # => 'nb'
|
49
|
-
# end
|
50
|
-
# page.locale # => 'en'
|
51
|
-
#
|
52
|
-
def localize(locale)
|
53
|
-
clone = self.clone.localize!(locale)
|
54
|
-
(block_given?) ? (yield clone) : clone
|
55
|
-
end
|
56
|
-
|
57
|
-
# In-place variant of #localize.
|
58
|
-
#
|
59
|
-
# page.localize!('en')
|
60
|
-
#
|
61
|
-
# This is functionally equivalent to setting locale=,
|
62
|
-
# but returns the model instead of the locale and is chainable.
|
63
|
-
#
|
64
|
-
def localize!(locale)
|
65
|
-
@localizer = Localizer.new(self)
|
66
|
-
localizer.locale = locale
|
67
|
-
self
|
68
|
-
end
|
69
|
-
|
70
|
-
# assign_attributes from ActiveRecord is overridden to catch locale before
|
71
|
-
# any other attributes are written. This enables the following construct:
|
72
|
-
#
|
73
|
-
# Page.create(name: 'My Page', locale: 'en')
|
74
|
-
#
|
75
|
-
def assign_attributes(new_attributes)
|
76
|
-
if new_attributes.is_a?(Hash)
|
77
|
-
attributes = new_attributes.stringify_keys
|
78
|
-
self.locale = attributes["language"] if attributes.key?("language")
|
79
|
-
self.locale = attributes["locale"] if attributes.key?("locale")
|
80
|
-
end
|
81
|
-
super
|
82
|
-
end
|
83
|
-
|
84
|
-
# A localized model responds to :foo, :foo= and :foo?
|
85
|
-
#
|
86
|
-
def respond_to?(method_name, *args)
|
87
|
-
requested_attribute, _ = method_name.to_s.match(/(.*?)([\?=]?)$/)[1..2]
|
88
|
-
localizer.attribute?(requested_attribute.to_sym) ? true : super
|
89
|
-
end
|
90
|
-
|
91
|
-
alias_method :translate, :localize
|
92
|
-
alias_method :translate!, :localize!
|
93
|
-
alias_method :working_language, :locale
|
94
|
-
alias_method :working_language=, :locale=
|
95
|
-
|
96
|
-
protected
|
97
|
-
|
98
|
-
# Getter for the model's Localizer.
|
99
|
-
#
|
100
|
-
def localizer
|
101
|
-
@localizer ||= Localizer.new(self)
|
102
|
-
end
|
103
|
-
|
104
|
-
# Callback for cleaning up empty localizations.
|
105
|
-
# This is performed automatically when the model is saved.
|
106
|
-
#
|
107
|
-
def cleanup_localizations!
|
108
|
-
localizer.cleanup_localizations!
|
109
|
-
end
|
110
|
-
|
111
|
-
def method_missing(method_name, *args)
|
112
|
-
attr = method_to_attr(method_name)
|
113
|
-
super unless localizer.attribute?(attr)
|
114
|
-
|
115
|
-
case method_name.to_s
|
116
|
-
when /\?$/
|
117
|
-
localizer.value_for?(attr)
|
118
|
-
when /=$/
|
119
|
-
localizer.set(attr, args.first)
|
120
|
-
else
|
121
|
-
localizer.get(attr).to_s
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
def method_to_attr(method_name)
|
126
|
-
method_name.to_s.match(/(.*?)([\?=]?)$/)[1].to_sym
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
@@ -1,72 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module PagesCore
|
4
|
-
module Localizable
|
5
|
-
class Localizer
|
6
|
-
attr_accessor :locale
|
7
|
-
def initialize(model)
|
8
|
-
@model = model
|
9
|
-
@configuration = model.class.localizable_configuration
|
10
|
-
end
|
11
|
-
|
12
|
-
def attribute?(attribute)
|
13
|
-
@configuration.attribute?(attribute)
|
14
|
-
end
|
15
|
-
|
16
|
-
def locales
|
17
|
-
@model.localizations.map(&:locale).uniq
|
18
|
-
end
|
19
|
-
|
20
|
-
def locale?
|
21
|
-
locale ? true : false
|
22
|
-
end
|
23
|
-
|
24
|
-
def get(attribute, options = {})
|
25
|
-
get_options = { locale: locale }.merge(options)
|
26
|
-
|
27
|
-
find_localizations(
|
28
|
-
attribute.to_s,
|
29
|
-
get_options[:locale].to_s
|
30
|
-
).try(&:first) ||
|
31
|
-
@model.localizations.new(
|
32
|
-
locale: get_options[:locale].to_s,
|
33
|
-
name: attribute.to_s
|
34
|
-
)
|
35
|
-
end
|
36
|
-
|
37
|
-
def set(attribute, value, options = {})
|
38
|
-
set_options = { locale: locale }.merge(options)
|
39
|
-
if value.is_a?(Hash)
|
40
|
-
value.each do |loc, val|
|
41
|
-
set(attribute, val, locale: loc)
|
42
|
-
end
|
43
|
-
else
|
44
|
-
unless set_options[:locale]
|
45
|
-
fail(
|
46
|
-
ArgumentError,
|
47
|
-
"Tried to set :#{attribute}, but no locale has been set"
|
48
|
-
)
|
49
|
-
end
|
50
|
-
get(attribute, locale: set_options[:locale]).value = value
|
51
|
-
end
|
52
|
-
value
|
53
|
-
end
|
54
|
-
|
55
|
-
def value_for?(attribute)
|
56
|
-
get(attribute).value?
|
57
|
-
end
|
58
|
-
|
59
|
-
def cleanup_localizations!
|
60
|
-
@model.localizations = @model.localizations.select(&:value?)
|
61
|
-
end
|
62
|
-
|
63
|
-
private
|
64
|
-
|
65
|
-
def find_localizations(name, locale)
|
66
|
-
@model.localizations.select do |l|
|
67
|
-
l.name == name && l.locale == locale
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module PagesCore
|
4
|
-
module Localizable
|
5
|
-
# = Localizable::ScopeExtension
|
6
|
-
#
|
7
|
-
# Injected into the Relation when Model.localized is called.
|
8
|
-
#
|
9
|
-
module ScopeExtension
|
10
|
-
attr_accessor :locale
|
11
|
-
|
12
|
-
def localize(locale)
|
13
|
-
@locale = locale
|
14
|
-
self
|
15
|
-
end
|
16
|
-
|
17
|
-
def to_a
|
18
|
-
super.map { |record| record.localize(@locale) }
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|