get rid of warnings about InstanceMethods (deprecated) + make the engine work smoother with an existing app + clean code

This commit is contained in:
did 2012-02-06 00:54:09 +01:00
parent 25e08596ef
commit fa36b95a9d
21 changed files with 317 additions and 314 deletions

View File

@ -1,15 +1,17 @@
class Locomotive::ContentLocalePickerCell < Cell::Base
module Locomotive
class ContentLocalePickerCell < Cell::Base
def show(args)
site = args[:site]
locale = args[:locale].to_s
def show(args)
site = args[:site]
locale = args[:locale].to_s
if site.locales.empty? || site.locales.size < 2
''
else
@locales = site.locales - [locale]
render
if site.locales.empty? || site.locales.size < 2
''
else
@locales = site.locales - [locale]
render
end
end
end
end
end

View File

@ -1,34 +1,36 @@
class Locomotive::GlobalActionsCell < ::Locomotive::MenuCell
module Locomotive
class GlobalActionsCell < MenuCell
attr_reader :current_locomotive_account, :current_site_url
attr_reader :current_locomotive_account, :current_site_url
def show(args)
@current_locomotive_account = args[:current_locomotive_account]
@current_site_url = args[:current_site_url]
super
end
protected
def build_list
add :welcome, :url => edit_my_account_url, :i18n_options => {
:key => 'locomotive.shared.header.welcome',
:arg => :name,
:value => @current_locomotive_account.name
}
add :see, :url => current_site_url, :id => 'viewsite', :target => '_blank'
if Locomotive.config.multi_sites? && current_locomotive_account.sites.size > 1
add :switch, :url => '#', :id => 'sites-picker-link'
def show(args)
@current_locomotive_account = args[:current_locomotive_account]
@current_site_url = args[:current_site_url]
super
end
add :help, :url => '#', :class => 'tutorial', :id => 'help'
add :logout, :url => destroy_locomotive_session_url, :confirm => t('locomotive.messages.confirm'), :method => :delete
end
protected
def localize_label(label, options = {})
I18n.t("locomotive.shared.header.#{label}", options)
end
def build_list
add :welcome, :url => edit_my_account_url, :i18n_options => {
:key => 'locomotive.shared.header.welcome',
:arg => :name,
:value => @current_locomotive_account.name
}
end
add :see, :url => current_site_url, :id => 'viewsite', :target => '_blank'
if Locomotive.config.multi_sites? && current_locomotive_account.sites.size > 1
add :switch, :url => '#', :id => 'sites-picker-link'
end
add :help, :url => '#', :class => 'tutorial', :id => 'help'
add :logout, :url => destroy_locomotive_session_url, :confirm => t('locomotive.messages.confirm'), :method => :delete
end
def localize_label(label, options = {})
I18n.t("locomotive.shared.header.#{label}", options)
end
end
end

View File

@ -1,10 +1,12 @@
class Locomotive::MainMenuCell < ::Locomotive::MenuCell
module Locomotive
class MainMenuCell < MenuCell
protected
protected
def build_list
add :contents, :url => pages_url
add :settings, :url => edit_current_site_url
end
def build_list
add :contents, :url => pages_url
add :settings, :url => edit_current_site_url
end
end
end

View File

@ -1,100 +1,102 @@
class Locomotive::MenuCell < Cell::Rails
module Locomotive
class MenuCell < Cell::Rails
include ::Locomotive::Engine.routes.url_helpers
include ::Locomotive::Engine.routes.url_helpers
delegate :sections, :to => :parent_controller
delegate :main_app, :sections, :to => :parent_controller
attr_accessor :list
attr_accessor :list
def initialize(*args)
super
self.list = []
end
def show(args = {})
self.build_list
render
end
def url_options
super.reverse_merge(
:host => request.host_with_port,
:protocol => request.protocol,
:_path_segments => request.symbolized_path_parameters
).merge(:script_name => request.script_name)
end
class MenuProxy
def initialize(cell)
@cell = cell
def initialize(*args)
super
self.list = []
end
def method_missing(meth, *args)
@cell.send(meth, *args)
def show(args = {})
self.build_list
render
end
end
def url_options
super.reverse_merge(
:host => request.host_with_port,
:protocol => request.protocol,
:_path_segments => request.symbolized_path_parameters
).merge(:script_name => request.script_name)
end
def self.update_for(name, &block)
method_name = "build_list_with_#{name}".to_sym
previous_method_name = "build_list_without_#{name}".to_sym
class MenuProxy
unless self.instance_methods.include?(method_name) # prevents the method to be called twice which will raise a "stack level too deep" exception
self.send(:define_method, method_name) do
self.send(previous_method_name)
block.call(MenuProxy.new(self))
def initialize(cell)
@cell = cell
end
# Note: this might cause "stack level too deep" if called twice for the same name
alias_method_chain :build_list, name.to_sym
def method_missing(meth, *args)
@cell.send(meth, *args)
end
end
end
protected
def self.update_for(name, &block)
method_name = "build_list_with_#{name}".to_sym
previous_method_name = "build_list_without_#{name}".to_sym
def build_list
raise 'the build_list method must be overridden'
end
unless self.instance_methods.include?(method_name) # prevents the method to be called twice which will raise a "stack level too deep" exception
def build_item(name, attributes)
unless attributes.key?(:label)
attributes[:label] = localize_label(name)
self.send(:define_method, method_name) do
self.send(previous_method_name)
block.call(MenuProxy.new(self))
end
# Note: this might cause "stack level too deep" if called twice for the same name
alias_method_chain :build_list, name.to_sym
end
end
attributes.merge!(:name => name, :class => name.to_s.dasherize.downcase)
end
protected
def add(name, attributes)
self.list << build_item(name, attributes)
end
def build_list
raise 'the build_list method must be overridden'
end
def add_after(pivot, name, attributes)
index = self.list.index { |i| i[:name] == pivot }
self.list.insert(index + 1, self.build_item(name, attributes))
end
def build_item(name, attributes)
unless attributes.key?(:label)
attributes[:label] = localize_label(name)
end
def add_before(pivot, name, attributes)
index = self.list.index { |i| i[:name] == pivot }
self.list.insert(index, self.build_item(name, attributes))
end
attributes.merge!(:name => name, :class => name.to_s.dasherize.downcase)
end
def modify(name, attributes)
self.find(name).merge!(attributes)
end
def add(name, attributes)
self.list << build_item(name, attributes)
end
def remove(name)
self.list.delete_if { |i| i[:name] == name }
end
def add_after(pivot, name, attributes)
index = self.list.index { |i| i[:name] == pivot }
self.list.insert(index + 1, self.build_item(name, attributes))
end
def find(name)
self.list.detect { |i| i[:name] == name }
end
def add_before(pivot, name, attributes)
index = self.list.index { |i| i[:name] == pivot }
self.list.insert(index, self.build_item(name, attributes))
end
def localize_label(label)
I18n.t("locomotive.shared.menu.#{label}")
end
def modify(name, attributes)
self.find(name).merge!(attributes)
end
end
def remove(name)
self.list.delete_if { |i| i[:name] == name }
end
def find(name)
self.list.detect { |i| i[:name] == name }
end
def localize_label(label)
I18n.t("locomotive.shared.menu.#{label}")
end
end
end

View File

@ -1,11 +1,13 @@
class Locomotive::SettingsMenuCell < ::Locomotive::SubMenuCell
module Locomotive
class SettingsMenuCell < SubMenuCell
protected
protected
def build_list
add :site, :url => edit_current_site_url
add :theme_assets, :url => theme_assets_url
add :account, :url => edit_my_account_url
end
def build_list
add :site, :url => edit_current_site_url
add :theme_assets, :url => theme_assets_url
add :account, :url => edit_my_account_url
end
end
end

View File

@ -1,11 +1,13 @@
class Locomotive::SubMenuCell < ::Locomotive::MenuCell
module Locomotive
class SubMenuCell < MenuCell
protected
protected
def build_item(name, attributes)
item = super
enhanced_class = "#{'on' if name.to_s == sections(:sub)} #{item[:class]}"
item.merge(:class => enhanced_class)
end
def build_item(name, attributes)
item = super
enhanced_class = "#{'on' if name.to_s == sections(:sub)} #{item[:class]}"
item.merge(:class => enhanced_class)
end
end
end

View File

@ -14,37 +14,33 @@ module Locomotive
validate :item_template_must_be_valid
end
module InstanceMethods
def item_template
@item_template ||= Marshal.load(read_attribute(:serialized_item_template).to_s) rescue nil
end
def item_template
@item_template ||= Marshal.load(read_attribute(:serialized_item_template).to_s) rescue nil
end
protected
protected
def serialize_item_template
if self.new_record? || self.raw_item_template_changed?
@item_parsing_errors = []
def serialize_item_template
if self.new_record? || self.raw_item_template_changed?
@item_parsing_errors = []
begin
self._parse_and_serialize_item_template
rescue ::Liquid::SyntaxError => error
@item_parsing_errors << I18n.t(:liquid_syntax, :error => error.to_s, :scope => [:errors, :messages])
end
begin
self._parse_and_serialize_item_template
rescue ::Liquid::SyntaxError => error
@item_parsing_errors << I18n.t(:liquid_syntax, :error => error.to_s, :scope => [:errors, :messages])
end
end
end
def _parse_and_serialize_item_template
item_template = ::Liquid::Template.parse(self.raw_item_template, {})
self.serialized_item_template = BSON::Binary.new(Marshal.dump(item_template))
def _parse_and_serialize_item_template
item_template = ::Liquid::Template.parse(self.raw_item_template, {})
self.serialized_item_template = BSON::Binary.new(Marshal.dump(item_template))
end
def item_template_must_be_valid
@item_parsing_errors.try(:each) do |msg|
%w(item_template raw_item_template).each { |field| self.errors.add field.to_sym, msg }
end
def item_template_must_be_valid
@item_parsing_errors.try(:each) do |msg|
%w(item_template raw_item_template).each { |field| self.errors.add field.to_sym, msg }
end
end
end
end

View File

@ -16,71 +16,49 @@ module Locomotive
end
module InstanceMethods
# Returns the fullpath of a page in the context of the current locale (I18n.locale)
# or the one passed in parameter. It also depends on the default site locale.
#
# Ex:
# For a site with its default site locale to 'en'
# # context 1: i18n.locale is 'en'
# contact_us.fullpath <= 'contact_us'
#
# # context 2: i18n.locale is 'fr'
# contact_us.fullpath <= 'fr/nous_contacter'
#
# @params [ Page ] page The page we want the localized fullpath
# @params [ String ] locale The optional locale in place of the current one
#
# @returns [ String ] The localized fullpath according to the current locale
#
def localized_page_fullpath(page, locale = nil)
locale = (locale || I18n.locale).to_s
fullpath = page.fullpath_translations[locale] || page.fullpath_translations[self.default_locale]
# Returns the fullpath of a page in the context of the current locale (I18n.locale)
# or the one passed in parameter. It also depends on the default site locale.
#
# Ex:
# For a site with its default site locale to 'en'
# # context 1: i18n.locale is 'en'
# contact_us.fullpath <= 'contact_us'
#
# # context 2: i18n.locale is 'fr'
# contact_us.fullpath <= 'fr/nous_contacter'
#
# @params [ Page ] page The page we want the localized fullpath
# @params [ String ] locale The optional locale in place of the current one
#
# @returns [ String ] The localized fullpath according to the current locale
#
def localized_page_fullpath(page, locale = nil)
locale = (locale || I18n.locale).to_s
fullpath = page.fullpath_translations[locale] || page.fullpath_translations[self.default_locale]
locale == self.default_locale ? fullpath : File.join(locale, fullpath)
end
locale == self.default_locale ? fullpath : File.join(locale, fullpath)
end
def locales=(array)
array = [] if array.blank?; super(array)
end
def locales=(array)
array = [] if array.blank?; super(array)
end
def default_locale
self.locales.first || Locomotive.config.site_locales.first
end
def default_locale
self.locales.first || Locomotive.config.site_locales.first
end
def locale_fallbacks(locale)
[locale.to_s] + (locales - [locale.to_s])
end
def locale_fallbacks(locale)
[locale.to_s] + (locales - [locale.to_s])
end
protected
def add_default_locale
self.locales = [Locomotive.config.site_locales.first] if self.locales.blank?
end
#
# def add_missing_locales_for_all_pages
# if self.locales_changed?
# list = self.pages.to_a
#
# while !list.empty? do
# page = list.pop
# begin
# page.send(:set_slug_and_fullpath_for_all_locales, self.locales)
#
# page.save
#
# rescue TypeError => e
# list.insert(0, page)
# end
# end
# end
# end
protected
def add_default_locale
self.locales = [Locomotive.config.site_locales.first] if self.locales.blank?
end
end
end
end
end

View File

@ -1,5 +1,5 @@
%h1
= link_to current_site.name, pages_url
= link_to current_site.name, locomotive.pages_url
= render_cell 'locomotive/global_actions', :show, :current_locomotive_account => current_locomotive_account, :current_site_url => current_site_public_url

View File

@ -3,8 +3,8 @@
- current_locomotive_account.sites.each do |site|
- unless current_site._id == site._id
%li
= link_to site.name, pages_url(:host => site.full_subdomain, :port => request.port)
= link_to site.name, locomotive.pages_url(:host => site.full_subdomain, :port => request.port)
- if can?(:manage, Locomotive::Site)
%p.action
= link_to t('locomotive.sites_picker.new'), new_site_url
= link_to t('locomotive.sites_picker.new'), locomotive.new_site_url

View File

@ -0,0 +1,19 @@
require 'active_support/dependencies'
module ActiveSupport::Dependencies
alias_method :require_or_load_without_multiple, :require_or_load
def require_or_load(file_name, const_path = nil)
if file_name.starts_with?(Rails.root.to_s + '/app')
relative_name = file_name.gsub(Rails.root.to_s, '')
@engine_paths ||= Rails::Application::Railties.engines.collect{|engine| engine.config.root.to_s }
@engine_paths.each do |path|
engine_file = File.join(path, relative_name)
require_or_load_without_multiple(engine_file, const_path) if File.file?(engine_file)
end
end
require_or_load_without_multiple(file_name, const_path)
end
end

View File

@ -2,4 +2,4 @@ require 'locomotive/action_controller/locale_helpers'
require 'locomotive/action_controller/section_helpers'
require 'locomotive/action_controller/url_helpers'
require 'locomotive/action_controller/responder'
require 'locomotive/action_controller/public_responder'
require 'locomotive/action_controller/public_responder'

View File

@ -8,42 +8,38 @@ module Locomotive
helper_method :current_content_locale
end
module InstanceMethods
def current_content_locale
::Mongoid::Fields::I18n.locale
end
def current_content_locale
::Mongoid::Fields::I18n.locale
def set_current_content_locale
if params[:content_locale].present?
session[:content_locale] = params[:content_locale]
end
def set_current_content_locale
if params[:content_locale].present?
session[:content_locale] = params[:content_locale]
end
unless current_site.locales.include?(session[:content_locale])
session[:content_locale] = current_site.default_locale
end
::Mongoid::Fields::I18n.locale = session[:content_locale]
self.setup_i18n_fallbacks
# logger.debug "*** content locale = #{session[:content_locale]} / #{::Mongoid::Fields::I18n.locale}"
unless current_site.locales.include?(session[:content_locale])
session[:content_locale] = current_site.default_locale
end
def set_back_office_locale
::I18n.locale = current_locomotive_account.locale rescue Locomotive.config.default_locale
end
::Mongoid::Fields::I18n.locale = session[:content_locale]
def back_to_default_site_locale
session[:content_locale] = ::Mongoid::Fields::I18n.locale = current_site.default_locale
end
self.setup_i18n_fallbacks
def setup_i18n_fallbacks
(current_site.locales || []).each do |locale|
::Mongoid::Fields::I18n.fallbacks_for(locale, current_site.locale_fallbacks(locale))
end
end
# logger.debug "*** content locale = #{session[:content_locale]} / #{::Mongoid::Fields::I18n.locale}"
end
def set_back_office_locale
::I18n.locale = current_locomotive_account.locale rescue Locomotive.config.default_locale
end
def back_to_default_site_locale
session[:content_locale] = ::Mongoid::Fields::I18n.locale = current_site.default_locale
end
def setup_i18n_fallbacks
(current_site.locales || []).each do |locale|
::Mongoid::Fields::I18n.fallbacks_for(locale, current_site.locale_fallbacks(locale))
end
end
end

View File

@ -4,16 +4,12 @@ module Locomotive
extend ActiveSupport::Concern
module InstanceMethods
def sections(key = nil)
if !key.nil? && key.to_sym == :sub
@locomotive_sections[:sub] || self.controller_name.dasherize
else
@locomotive_sections[:main]
end
def sections(key = nil)
if !key.nil? && key.to_sym == :sub
@locomotive_sections[:sub] || self.controller_name.dasherize
else
@locomotive_sections[:main]
end
end
module ClassMethods

View File

@ -8,33 +8,30 @@ module Locomotive
helper_method :current_site_public_url, :switch_to_site_url, :public_page_url
end
module InstanceMethods
def current_site_public_url
request.protocol + request.host_with_port
end
def current_site_public_url
request.protocol + request.host_with_port
def switch_to_site_url(site, options = {})
options = { :fullpath => true, :protocol => true }.merge(options)
url = "#{site.subdomain}.#{Locomotive.config.domain}"
url += ":#{request.port}" if request.port != 80
url = File.join(url, request.fullpath) if options[:fullpath]
url = "http://#{url}" if options[:protocol]
url
end
def public_page_url(page, options = {})
if content = options.delete(:content)
File.join(current_site_public_url, page.fullpath.gsub('content_type_template', ''), content._slug)
else
File.join(current_site_public_url, page.fullpath)
end
def switch_to_site_url(site, options = {})
options = { :fullpath => true, :protocol => true }.merge(options)
url = "#{site.subdomain}.#{Locomotive.config.domain}"
url += ":#{request.port}" if request.port != 80
url = File.join(url, request.fullpath) if options[:fullpath]
url = "http://#{url}" if options[:protocol]
url
end
def public_page_url(page, options = {})
if content = options.delete(:content)
File.join(current_site_public_url, page.fullpath.gsub('content_type_template', ''), content._slug)
else
File.join(current_site_public_url, page.fullpath)
end
end
end
end
end
end

View File

@ -12,46 +12,43 @@ module Locomotive
end
end
module InstanceMethods
protected
protected
def fetch_site
Locomotive.log "[fetch site] host = #{request.host} / #{request.env['HTTP_HOST']}"
def fetch_site
Locomotive.log "[fetch site] host = #{request.host} / #{request.env['HTTP_HOST']}"
if Locomotive.config.multi_sites?
@current_site ||= Site.match_domain(request.host).first
else
@current_site ||= Site.first
end
if Locomotive.config.multi_sites?
@current_site ||= Site.match_domain(request.host).first
else
@current_site ||= Site.first
end
end
def current_site
@current_site || fetch_site
end
def current_site
@current_site || fetch_site
end
def require_site
return true if current_site
def require_site
return true if current_site
redirect_to installation_url and return false if Locomotive::Account.count == 0 || Locomotive::Site.count == 0
redirect_to installation_url and return false if Locomotive::Account.count == 0 || Locomotive::Site.count == 0
render_no_site_error and return false
end
render_no_site_error and return false
end
def render_no_site_error
render :template => '/Locomotive/errors/no_site', :layout => false, :status => :not_found
end
def render_no_site_error
render :template => '/Locomotive/errors/no_site', :layout => false, :status => :not_found
end
def validate_site_membership
return true if current_site.present? && current_site.accounts.include?(current_locomotive_account)
sign_out(current_locomotive_account)
flash[:alert] = I18n.t(:no_membership, :scope => [:devise, :failure, :locomotive])
redirect_to new_locomotive_account_session_url and return false
end
def validate_site_membership
return true if current_site.present? && current_site.accounts.include?(current_locomotive_account)
sign_out(current_locomotive_account)
flash[:alert] = I18n.t(:no_membership, :scope => [:devise, :failure, :locomotive])
redirect_to new_locomotive_account_session_url and return false
end
end
end
end

View File

@ -29,17 +29,12 @@ Gem::Specification.new do |s|
s.add_dependency 'bson_ext', '~> 1.5.2'
s.add_dependency 'mongoid', '~> 2.4.3'
s.add_dependency 'locomotive-mongoid-tree', '~> 0.6.2'
# s.add_dependency 'locomotive_mongoid_acts_as_tree', '~> 0.1.5.8'
s.add_dependency 'custom_fields', '~> 2.0.0.rc3'
s.add_dependency 'kaminari', '~> 0.13.0'
s.add_dependency 'haml', '~> 3.1.4'
# # s.add_dependency 'sass-rails', '~> 3.1.5'
# # s.add_dependency 'coffee-script', '~> 2.2.0'
# # s.add_dependency 'uglifier', '~> 1.2.2'
# # # s.add_dependency 'compass', '~> 0.12.rc.0'
s.add_dependency 'jquery-rails', '~> 1.0.16'
s.add_dependency 'rails-backbone', '~> 0.6.1'
s.add_dependency 'codemirror-rails', '~> 2.21'

View File

@ -0,0 +1,9 @@
module Locomotive
class MainMenuCell < MenuCell
update_for :foo do |new_menu|
new_menu.add_before :settings, :foo, :url => new_menu.main_app.foo_url
end
end
end

View File

@ -1,8 +1,10 @@
class FooController < ApplicationController
class FooController < Locomotive::BaseController
skip_load_and_authorize_resource
sections :foo
def index
render :text => 'Foo'
end
end

View File

@ -0,0 +1 @@
%p Hello world !!!

View File

@ -3,3 +3,8 @@
en:
hello: "Hello world"
locomotive:
shared:
menu:
foo: My FOO menu