Skip to content

Commit

Permalink
incorporate Engine into chanko
Browse files Browse the repository at this point in the history
  • Loading branch information
eudoxa committed Apr 24, 2012
1 parent 46829f0 commit d6226c1
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 4 deletions.
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,6 @@
Chanko::Engine.routes.draw do
end

Chanko::Application.routes.draw do
mount Chanko::Engine => "/ext"
end
6 changes: 5 additions & 1 deletion lib/chanko.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,7 @@
# singletone issue will be fixed
# https://github.com/radar/forem/commit/049057a8e94e59610365b6764c733eb7876c8351
require 'active_support/core_ext/kernel/singleton_class'
require 'chanko/engine'

module Chanko module Chanko
require 'chanko/railtie'
end end
2 changes: 1 addition & 1 deletion lib/chanko/railtie.rb → lib/chanko/engine.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Chanko
autoload klass.camelize, "chanko/#{klass}" autoload klass.camelize, "chanko/#{klass}"
end end


class Railtie < Rails::Railtie class Engine < ::Rails::Engine
initializer 'chanko.attach' do |app| initializer 'chanko.attach' do |app|
::ActionView::Base.send(:include, Chanko::Invoker) ::ActionView::Base.send(:include, Chanko::Invoker)
::ActionController::Base.send(:include, Chanko::Invoker) ::ActionController::Base.send(:include, Chanko::Invoker)
Expand Down
11 changes: 10 additions & 1 deletion lib/chanko/loader.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ def aborted_units
self.__aborted.uniq self.__aborted.uniq
end end



def load_expander(unit_name) def load_expander(unit_name)
%w(models helpers).each do |targets| %w(models helpers).each do |targets|
Chanko::Loader.directories.each do |directory| Chanko::Loader.directories.each do |directory|
Expand Down Expand Up @@ -152,11 +151,21 @@ def deregister(obj)


def load_path_file(file, root) def load_path_file(file, root)
@directories = Chanko::Directories.load_path_file(file, root) @directories = Chanko::Directories.load_path_file(file, root)
initialize_engines
end end


def add_path(path) def add_path(path)
@directories ||= Chanko::Directories.new @directories ||= Chanko::Directories.new
@directories.add(path) @directories.add(path)
initialize_engines
end

def initialize_engines
Chanko::Loader.directories.each do |directory|
Pathname.glob("#{directory}/*/lib/*/engine.rb").each do |engine|
require engine.sub(/\.rb$/, '')
end
end
end end


def remove_path(path) def remove_path(path)
Expand Down
2 changes: 2 additions & 0 deletions spec/app.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class CreateAllTables < ::ActiveRecord::Migration
def self.up def self.up
create_table(:users) {|t| t.string :name } create_table(:users) {|t| t.string :name }
create_table(:recipes) {|t| t.string :title; t.integer :user_id } create_table(:recipes) {|t| t.string :title; t.integer :user_id }
create_table(:diaries) {|t| t.string :content; t.integer :user_id }
create_table(:comments) {|t| t.string :content; t.integer :user_id }
end end
end end


1 change: 1 addition & 0 deletions spec/app/route.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,4 @@
Chanko::Application.routes.draw do Chanko::Application.routes.draw do
mount Chanko::Engine => "/"
match '/:controller(/:action(/:id))' match '/:controller(/:action(/:id))'
end end
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,7 @@
module EngineUnit
class TopController < ActionController::Base
def top
render :text => 'hoge'
end
end
end
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,8 @@
module EngineUnit
class Comment < ActiveRecord::Base
set_table_name :comments
def hello
'hello'
end
end
end
8 changes: 8 additions & 0 deletions spec/fixtures/test_units/engine_unit/config/routes.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,8 @@
EngineUnit::Engine.routes.draw do
match '/' => 'top#top'
end

Chanko::Engine.routes.draw do
mount EngineUnit::Engine => "/engine_unit"
end

Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
module EngineUnit
class Engine < ::Rails::Engine
isolate_namespace EngineUnit
end
end
13 changes: 13 additions & 0 deletions spec/integration/engine_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'spec_helper'

describe "Chanko", :type => :integration do
describe "Engine" do
before do
end

it 'access engine action' do
visit "/engine_unit"
(response || page).body.should == 'hoge'
end
end
end
27 changes: 27 additions & 0 deletions spec/lib/engine_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'spec_helper'

describe Chanko do
shared_examples_for 'engine' do
describe 'model' do
it 'use engine model method' do
EngineUnit::Comment.new.hello.should == 'hello'
end

it'save engine model' do
expect {
EngineUnit::Comment.create
}.to change(EngineUnit::Comment, :count).from(0).to(1)
end
end
end

context 'with cache_classes' do
before { Chanko.config.cache_classes = true }
it_should_behave_like 'engine'
end

context 'without cache_classes' do
before { Chanko.config.cache_classes = false }
it_should_behave_like 'engine'
end
end
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@


require 'chanko' require 'chanko'
require 'haml' require 'haml'
require 'fixtures/test_units/engine_unit/lib/engine_unit/engine'
require 'app' require 'app'


#Rails.root = Fir.dirname(__FILE__) #Rails.root = Fir.dirname(__FILE__)
Expand Down Expand Up @@ -45,7 +46,7 @@ def fixtures_path
config.before(:suite) do config.before(:suite) do
$: << fixtures_path.join('lib') $: << fixtures_path.join('lib')
path = fixtures_path.join('test_units') path = fixtures_path.join('test_units')
Chanko::Loader.directories.unshift(path) Chanko::Loader.add_path(path)
Chanko::ActiveIf.files = [fixtures_path.join('active_if', "main")] Chanko::ActiveIf.files = [fixtures_path.join('active_if', "main")]
ApplicationController.send(:include, Chanko::Invoker) ApplicationController.send(:include, Chanko::Invoker)
ActionView::Base.send(:include, Chanko::Invoker) ActionView::Base.send(:include, Chanko::Invoker)
Expand Down

0 comments on commit d6226c1

Please sign in to comment.