From d856be1064635df759445fb43c000b0b6b5557f5 Mon Sep 17 00:00:00 2001 From: Jamie Winsor Date: Mon, 19 Sep 2011 16:47:55 -0700 Subject: [PATCH 1/6] Refactor core/engine into it's own file --- core/lib/refinery/core/engine.rb | 82 ++++++++++++++++++++++++++++++++ core/lib/refinerycms-core.rb | 81 +------------------------------ 2 files changed, 83 insertions(+), 80 deletions(-) create mode 100644 core/lib/refinery/core/engine.rb diff --git a/core/lib/refinery/core/engine.rb b/core/lib/refinery/core/engine.rb new file mode 100644 index 0000000000..7950b9c6c3 --- /dev/null +++ b/core/lib/refinery/core/engine.rb @@ -0,0 +1,82 @@ +module Refinery + module Core + class Engine < ::Rails::Engine + isolate_namespace ::Refinery + + config.autoload_paths += %W( #{config.root}/lib ) + + # Attach ourselves to the Rails application. + config.before_configuration do + ::Refinery::Core.attach_to_application! + end + + # Wrap errors in spans and cache vendored assets. + config.to_prepare do + # This wraps errors in span not div + ActionView::Base.field_error_proc = Proc.new do |html_tag, instance| + "#{html_tag}".html_safe + end + end + + # set per_page globally + config.to_prepare do + WillPaginate.per_page = 20 + end + + # Register the plugin + config.after_initialize do + ::Refinery::Plugin.register do |plugin| + plugin.pathname = root + plugin.name = 'refinery_core' + plugin.class_name = 'RefineryEngine' + plugin.version = ::Refinery.version + plugin.hide_from_menu = true + plugin.always_allow_access = true + plugin.menu_match = /refinery\/(refinery_core)$/ + end + + # Register the dialogs plugin + ::Refinery::Plugin.register do |plugin| + plugin.pathname = root + plugin.name = 'refinery_dialogs' + plugin.version = ::Refinery.version + plugin.hide_from_menu = true + plugin.always_allow_access = true + plugin.menu_match = /refinery\/(refinery_)?dialogs/ + end + end + + initializer 'add catch all routes' do |app| + app.routes_reloader.paths << File.expand_path('../refinery/catch_all_routes.rb', __FILE__) + end + + initializer 'add presenters' do |app| + app.config.autoload_paths += [ + Rails.root.join('app', 'presenters'), + Rails.root.join('vendor', '**', '**', 'app', 'presenters'), + Refinery.roots.map{|r| r.join('**', 'app', 'presenters')} + ].flatten + end + + initializer 'configure acts_as_indexed' do |app| + ActsAsIndexed.configure do |config| + config.index_file = Rails.root.join('tmp', 'index') + config.index_file_depth = 3 + config.min_word_size = 3 + end + end + + initializer "refinery.assets.precompile" do |app| + app.config.assets.precompile += [ + "refinery/*", + "refinery/icons/*", + "wymeditor/lang/*", + "wymeditor/skins/refinery/*", + "wymeditor/skins/refinery/**/*", + "modernizr-min.js", + "dd_belatedpng.js" + ] + end + end + end +end diff --git a/core/lib/refinerycms-core.rb b/core/lib/refinerycms-core.rb index a2a7ad9197..854b04a837 100644 --- a/core/lib/refinerycms-core.rb +++ b/core/lib/refinerycms-core.rb @@ -56,87 +56,8 @@ def root end ::Rails::Engine.send :include, ::Refinery::Engine - - class Engine < ::Rails::Engine - isolate_namespace ::Refinery - - config.autoload_paths += %W( #{config.root}/lib ) - - # Attach ourselves to the Rails application. - config.before_configuration do - ::Refinery::Core.attach_to_application! - end - - # Wrap errors in spans and cache vendored assets. - config.to_prepare do - # This wraps errors in span not div - ActionView::Base.field_error_proc = Proc.new do |html_tag, instance| - "#{html_tag}".html_safe - end - end - - # set per_page globally - config.to_prepare do - WillPaginate.per_page = 20 - end - - # Register the plugin - config.after_initialize do - ::Refinery::Plugin.register do |plugin| - plugin.pathname = root - plugin.name = 'refinery_core' - plugin.class_name = 'RefineryEngine' - plugin.version = ::Refinery.version - plugin.hide_from_menu = true - plugin.always_allow_access = true - plugin.menu_match = /refinery\/(refinery_core)$/ - end - - # Register the dialogs plugin - ::Refinery::Plugin.register do |plugin| - plugin.pathname = root - plugin.name = 'refinery_dialogs' - plugin.version = ::Refinery.version - plugin.hide_from_menu = true - plugin.always_allow_access = true - plugin.menu_match = /refinery\/(refinery_)?dialogs/ - end - end - - initializer 'add catch all routes' do |app| - app.routes_reloader.paths << File.expand_path('../refinery/catch_all_routes.rb', __FILE__) - end - - initializer 'add presenters' do |app| - app.config.autoload_paths += [ - Rails.root.join('app', 'presenters'), - Rails.root.join('vendor', '**', '**', 'app', 'presenters'), - Refinery.roots.map{|r| r.join('**', 'app', 'presenters')} - ].flatten - end - - initializer 'configure acts_as_indexed' do |app| - ActsAsIndexed.configure do |config| - config.index_file = Rails.root.join('tmp', 'index') - config.index_file_depth = 3 - config.min_word_size = 3 - end - end - - initializer "refinery.assets.precompile" do |app| - app.config.assets.precompile += [ - "refinery/*", - "refinery/icons/*", - "wymeditor/lang/*", - "wymeditor/skins/refinery/*", - "wymeditor/skins/refinery/**/*", - "modernizr-min.js", - "dd_belatedpng.js" - ] - end - end end - end +require 'refinery/core/engine' ::Refinery.engines << 'core' From 749880842ffa02d2bde8a8b955f678ff1d46d1aa Mon Sep 17 00:00:00 2001 From: Jamie Winsor Date: Mon, 19 Sep 2011 17:36:28 -0700 Subject: [PATCH 2/6] Decorators in app/decorators will now be loaded at application start --- Gemfile.lock | 4 ++-- core/lib/refinery/core/engine.rb | 12 ++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 200e00bbab..1307c3af3f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -31,7 +31,7 @@ PATH friendly_id_globalize3 (~> 3.2.1) globalize3 (~> 0.2.0.beta3) jquery-rails - rails (>= 3.1.0) + rails (>= 3.1.1.rc1) refinerycms-base (= 2.0.0) refinerycms-settings (= 2.0.0) sass-rails (~> 3.1.0) @@ -267,7 +267,7 @@ GEM multi_json (>= 1.0.2) warden (1.0.5) rack (>= 1.0) - will_paginate (3.0.0) + will_paginate (3.0.1) xpath (0.1.4) nokogiri (~> 1.3) diff --git a/core/lib/refinery/core/engine.rb b/core/lib/refinery/core/engine.rb index 7950b9c6c3..646463dcf7 100644 --- a/core/lib/refinery/core/engine.rb +++ b/core/lib/refinery/core/engine.rb @@ -2,13 +2,21 @@ module Refinery module Core class Engine < ::Rails::Engine isolate_namespace ::Refinery - + + def self.load_decorators + Dir.glob(File.join(Rails.root, "app/decorators/**/*_decorator.rb")) do |c| + require(c) + end + end + config.autoload_paths += %W( #{config.root}/lib ) # Attach ourselves to the Rails application. config.before_configuration do ::Refinery::Core.attach_to_application! end + + refinery.after_inclusion &method(:load_decorators).to_proc # Wrap errors in spans and cache vendored assets. config.to_prepare do @@ -47,7 +55,7 @@ class Engine < ::Rails::Engine end initializer 'add catch all routes' do |app| - app.routes_reloader.paths << File.expand_path('../refinery/catch_all_routes.rb', __FILE__) + app.routes_reloader.paths << File.expand_path('../../catch_all_routes.rb', __FILE__) end initializer 'add presenters' do |app| From 4f712d89348b20e1c191e9dd8542389a78043252 Mon Sep 17 00:00:00 2001 From: Jamie Winsor Date: Mon, 19 Sep 2011 18:22:41 -0700 Subject: [PATCH 3/6] Add generator steps to generate decorator directories Improve test coverage for CmsGenerator --- core/lib/generators/cms_generator.rb | 26 ++++++---- .../decorators/controllers/refinery/.gitkeep | 0 .../app/decorators/models/refinery/.gitkeep | 0 .../spec/lib/generators/cms_generator_spec.rb | 49 +++++++++++++++++++ 4 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 core/lib/generators/templates/app/decorators/controllers/refinery/.gitkeep create mode 100644 core/lib/generators/templates/app/decorators/models/refinery/.gitkeep create mode 100644 core/spec/lib/generators/cms_generator_spec.rb diff --git a/core/lib/generators/cms_generator.rb b/core/lib/generators/cms_generator.rb index 461127a168..db72d1aa63 100644 --- a/core/lib/generators/cms_generator.rb +++ b/core/lib/generators/cms_generator.rb @@ -14,7 +14,7 @@ def generate self.silence_puts = true # Only pretend to do the next actions if this is Refinery to stay DRY - if Rails.root == Refinery.root + if destination_path == Refinery.root.to_s say_status :'-- pretending to make changes that happen in an actual installation --', nil, :yellow old_pretend = self.options[:pretend] new_options = self.options.dup @@ -25,7 +25,7 @@ def generate unless self.options[:update] # First, effectively move / rename files that get in the way of Refinery CMS %w(public/index.html app/views/layouts/application.html.erb).each do |roadblock| - if (roadblock_path = Rails.root.join(roadblock)).file? + if (roadblock_path = destination_path.join(roadblock)).file? create_file "#{roadblock}.backup", :verbose => true do roadblock_path.read end remove_file roadblock_path, :verbose => true @@ -35,9 +35,11 @@ def generate # Massage environment files %w(development test production).map{|e| "config/environments/#{e}.rb"}.each do |env| + next unless destination_path.join(env).file? + gsub_file env, "config.assets.compile = false", "config.assets.compile = true", :verbose => false - unless (env_file_contents = Rails.root.join(env).read) =~ %r{Refinery.rescue_not_found} + unless (env_file_contents = destination_path.join(env).read) =~ %r{Refinery.rescue_not_found} append_file env, "Refinery.rescue_not_found = #{env.split('/').last.split('.rb').first == 'production'}\n" end @@ -50,7 +52,7 @@ def generate end # Stop pretending - if Rails.root == Refinery.root + if destination_path == Refinery.root.to_s say_status :'-- finished pretending --', nil, :yellow new_options = self.options.dup new_options[:pretend] = old_pretend @@ -58,13 +60,13 @@ def generate end # Ensure .gitignore exists and append our rules to it. - create_file ".gitignore" unless Rails.root.join('.gitignore').file? + create_file ".gitignore" unless destination_path.join('.gitignore').file? our_ignore_rules = self.class.source_root.join('.gitignore').read - our_ignore_rules = our_ignore_rules.split('# REFINERY CMS DEVELOPMENT').first if Rails.root != Refinery.root + our_ignore_rules = our_ignore_rules.split('# REFINERY CMS DEVELOPMENT').first if destination_path != Refinery.root.to_s append_file ".gitignore", our_ignore_rules # If the admin/base_controller.rb file exists, ensure it does not do the old inheritance - if (admin_base = Rails.root.join('app', 'controllers', 'refinery', 'admin_controller.rb')).file? + if (admin_base = destination_path.join('app', 'controllers', 'refinery', 'admin_controller.rb')).file? gsub_file admin_base, "# You can extend refinery backend with your own functions here and they will likely not get overriden in an update.", "", @@ -75,7 +77,7 @@ def generate end # Append seeds. - create_file "db/seeds.rb" unless Rails.root.join('db', 'seeds.rb').file? + create_file "db/seeds.rb" unless destination_path.join('db', 'seeds.rb').file? append_file 'db/seeds.rb', :verbose => true do self.class.source_root.join('db', 'seeds.rb').read end @@ -97,7 +99,13 @@ def generate Pathname.glob(self.class.source_root.join('**', '*')).reject{|f| f.directory? or f.to_s =~ /\/db\// }.sort.each do |path| - copy_file path, path.to_s.gsub(self.class.source_root.to_s, Rails.root.to_s) + copy_file path, path.to_s.gsub(self.class.source_root.to_s, destination_path.to_s) + end + + # Create decorator directories + ['controllers', 'models'].each do |decorator_namespace| + src_file_path = "app/decorators/#{decorator_namespace}/refinery/.gitkeep" + copy_file self.class.source_root.join(src_file_path), destination_path.join(src_file_path) end # Ensure i18n exists and is up to date. diff --git a/core/lib/generators/templates/app/decorators/controllers/refinery/.gitkeep b/core/lib/generators/templates/app/decorators/controllers/refinery/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core/lib/generators/templates/app/decorators/models/refinery/.gitkeep b/core/lib/generators/templates/app/decorators/models/refinery/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core/spec/lib/generators/cms_generator_spec.rb b/core/spec/lib/generators/cms_generator_spec.rb new file mode 100644 index 0000000000..df8020d0e1 --- /dev/null +++ b/core/spec/lib/generators/cms_generator_spec.rb @@ -0,0 +1,49 @@ +require 'spec_helper' +require "generator_spec/test_case" + +module Refinery + describe CmsGenerator do + include GeneratorSpec::TestCase + destination File.expand_path("../../tmp", __FILE__) + + before(:each) do + prepare_destination + run_generator + end + + specify do + destination_root.should have_structure { + directory "app" do + directory "decorators" do + directory "controllers" do + directory "refinery" do + file ".gitkeep" + end + end + directory "models" do + directory "refinery" do + file ".gitkeep" + end + end + end + directory "views" do + directory "sitemap" do + file "index.xml.builder" + end + end + end + directory "db" do + file "seeds.rb" + end + directory "config" do + file "database.yml.mysql" + file "database.yml.postgresql" + file "database.yml.sqlite3" + directory "initializers" do + file "devise.rb" + end + end + } + end + end +end From 8c52ac6a54d8bb28f1ea339199e7be760bdd951a Mon Sep 17 00:00:00 2001 From: Jamie Winsor Date: Mon, 19 Sep 2011 19:13:06 -0700 Subject: [PATCH 4/6] Update gemspec --- core/refinerycms-core.gemspec | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/refinerycms-core.gemspec b/core/refinerycms-core.gemspec index 7abf485623..696014582b 100644 --- a/core/refinerycms-core.gemspec +++ b/core/refinerycms-core.gemspec @@ -356,6 +356,11 @@ Gem::Specification.new do |s| 'lib/generators/templates', 'lib/generators/templates/.gitignore', 'lib/generators/templates/app', + 'lib/generators/templates/app/decorators', + 'lib/generators/templates/app/decorators/controllers', + 'lib/generators/templates/app/decorators/controllers/refinery', + 'lib/generators/templates/app/decorators/models', + 'lib/generators/templates/app/decorators/models/refinery', 'lib/generators/templates/app/views', 'lib/generators/templates/app/views/sitemap', 'lib/generators/templates/app/views/sitemap/index.xml.builder', @@ -380,6 +385,8 @@ Gem::Specification.new do |s| 'lib/refinery/base_presenter.rb', 'lib/refinery/catch_all_routes.rb', 'lib/refinery/configuration.rb', + 'lib/refinery/core', + 'lib/refinery/core/engine.rb', 'lib/refinery/crud.rb', 'lib/refinery/engine.rb', 'lib/refinery/helpers', @@ -422,6 +429,8 @@ Gem::Specification.new do |s| 'spec/controllers/refinery/fast_controller_spec.rb', 'spec/controllers/refinery/sitemap_controller_spec.rb', 'spec/lib', + 'spec/lib/generators', + 'spec/lib/generators/cms_generator_spec.rb', 'spec/lib/refinery', 'spec/lib/refinery/activity_spec.rb', 'spec/lib/refinery/plugin_spec.rb', From b5125606a6acc5362210499850c23f4c1686f446 Mon Sep 17 00:00:00 2001 From: Jamie Winsor Date: Tue, 20 Sep 2011 18:08:22 -0700 Subject: [PATCH 5/6] Fix decorator not decorating controllers after first request when cache_classes is false --- core/lib/refinery/core/engine.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/lib/refinery/core/engine.rb b/core/lib/refinery/core/engine.rb index 646463dcf7..538e07c2f4 100644 --- a/core/lib/refinery/core/engine.rb +++ b/core/lib/refinery/core/engine.rb @@ -5,7 +5,7 @@ class Engine < ::Rails::Engine def self.load_decorators Dir.glob(File.join(Rails.root, "app/decorators/**/*_decorator.rb")) do |c| - require(c) + Rails.application.config.cache_classes ? require(c) : load(c) end end From d92aab91333f5e902dced560be475e005159a1e9 Mon Sep 17 00:00:00 2001 From: Jamie Winsor Date: Tue, 20 Sep 2011 18:38:32 -0700 Subject: [PATCH 6/6] Remove unnecessary type change on pathname to string --- core/lib/generators/cms_generator.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/lib/generators/cms_generator.rb b/core/lib/generators/cms_generator.rb index db72d1aa63..060b314417 100644 --- a/core/lib/generators/cms_generator.rb +++ b/core/lib/generators/cms_generator.rb @@ -14,7 +14,7 @@ def generate self.silence_puts = true # Only pretend to do the next actions if this is Refinery to stay DRY - if destination_path == Refinery.root.to_s + if destination_path == Refinery.root say_status :'-- pretending to make changes that happen in an actual installation --', nil, :yellow old_pretend = self.options[:pretend] new_options = self.options.dup @@ -52,7 +52,7 @@ def generate end # Stop pretending - if destination_path == Refinery.root.to_s + if destination_path == Refinery.root say_status :'-- finished pretending --', nil, :yellow new_options = self.options.dup new_options[:pretend] = old_pretend @@ -62,7 +62,7 @@ def generate # Ensure .gitignore exists and append our rules to it. create_file ".gitignore" unless destination_path.join('.gitignore').file? our_ignore_rules = self.class.source_root.join('.gitignore').read - our_ignore_rules = our_ignore_rules.split('# REFINERY CMS DEVELOPMENT').first if destination_path != Refinery.root.to_s + our_ignore_rules = our_ignore_rules.split('# REFINERY CMS DEVELOPMENT').first if destination_path != Refinery.root append_file ".gitignore", our_ignore_rules # If the admin/base_controller.rb file exists, ensure it does not do the old inheritance