Skip to content

Commit

Permalink
Merge pull request #78 from eudoxa/no_longer_add_unit_path_to_view_paths
Browse files Browse the repository at this point in the history
[Breaking change] Chanko no longer modifies view_paths to add or remove its own view path.
  • Loading branch information
eudoxa committed Mar 7, 2024
2 parents 8f3fbf2 + ab758dd commit a544457
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 80 deletions.
16 changes: 1 addition & 15 deletions README.md
Expand Up @@ -63,11 +63,6 @@ end
= invoke(:example_unit, :render_example)
```

```
-# app/units/example_unit/views/_example.html.slim
= foo
```

## Unit
You can see [the real example of an unit module file](https://github.com/cookpad/chanko/blob/master/spec/dummy/app/units/entry_deletion/entry_deletion.rb).

Expand Down Expand Up @@ -120,16 +115,7 @@ end
```

### render
The view path app/units/example_unit/views is added into view_paths in invoking.
So you can render app/units/example_unit/views/_example.html.slim in invoking.

```ruby
scope(:view) do
function(:render_example) do
render "/example", :foo => hello("world")
end
end
```
In version 2 and earlier, Chanko extended Rails' search path to include the views path of the unit. This functionality has been discontinued. To maintain the views path under the unit, you will need to manually create a symbolic link in app/views/units to access it.

### models
In models block, you can expand model features by `expand` method.
Expand Down
17 changes: 4 additions & 13 deletions lib/chanko/function.rb
Expand Up @@ -22,12 +22,10 @@ def initialize(unit, label, &block)

def invoke(context, options = {})
with_unit_stack(context) do
with_unit_view_path(context) do
capture_exception(context) do
result = context.instance_eval(&block)
result = decorate(result, context, options[:type]) if context.view? && result.present?
result
end
capture_exception(context) do
result = context.instance_eval(&block)
result = decorate(result, context, options[:type]) if context.view? && result.present?
result
end
end
end
Expand Down Expand Up @@ -59,13 +57,6 @@ def with_unit_stack(context)
context.units.pop
end

def with_unit_view_path(context)
context.view_paths.unshift unit.resolver if context.respond_to?(:view_paths)
yield
ensure
context.view_paths.paths.shift if context.respond_to?(:view_paths)
end

def capture_exception(context)
yield
rescue Exception => exception
Expand Down
8 changes: 0 additions & 8 deletions lib/chanko/unit.rb
Expand Up @@ -70,10 +70,6 @@ def to_prefix
UnitProxy.generate_prefix(unit_name)
end

def view_path
Rails.root.join("#{Config.units_directory_path}/#{unit_name}/views").to_s
end

def find_function(identifier, label)
scope = ScopeFinder.find(identifier)
target = scope.ancestors.find {|klass| scopes[klass] }
Expand All @@ -93,10 +89,6 @@ def shared_methods
@shared_methods ||= {}
end

def resolver
@resolver ||= Config.resolver.new(view_path)
end

def extender
@extender ||= Extender.new(to_prefix)
end
Expand Down
6 changes: 2 additions & 4 deletions lib/generators/chanko/unit/templates/unit.rb.erb
Expand Up @@ -34,13 +34,11 @@ module <%= class_name %>
# ```

# ## render
# In addition, the view path "<%= "#{directory}/views" %>" is added into view_paths.
# So you can render <%= "#{directory}/views/_example.html.erb" %> in invoking.
#
# If you want to place the views path under the unit, create a symbolic link under app/views/units and refer to it.
# ```
# scope(:view) do
# function(:function_name) do
# render "/example", :foo => "bar"
# render "/units/<%= your_unit_name.underscore %>/example", :foo => "bar"
# end
# end
# ```
Expand Down
32 changes: 0 additions & 32 deletions spec/chanko/function_spec.rb
Expand Up @@ -15,10 +15,6 @@ def current_unit
def units
@units ||= []
end

def path
view_paths.first.to_s
end
end
klass.new
end
Expand All @@ -32,12 +28,7 @@ def current_unit
def units
@units ||= []
end

def path
view_paths.first.to_s
end
end

klass.with_view_paths([], {}, nil)
end

Expand All @@ -50,16 +41,6 @@ def path
end
end

let(:context_without_view_paths) do
Class.new do
include Chanko::Invoker

def units
@units ||= []
end
end.new
end

let(:options) do
{ :type => :plain }
end
Expand All @@ -68,19 +49,6 @@ def units
it "invokes block with given context and stacked unit" do
expect(described_class.new(unit, :label) { current_unit }.invoke(context, options)).to eq(unit)
end

context "when context is a view" do
it "invokes with unit's view path" do
expect(described_class.new(unit, :label) { path }.invoke(context, options)).to eq(unit.view_path)
end
end

context "when context does not have view_paths" do
it "invokes successfully" do
expect(described_class.new(unit, :label) { "test" }.
invoke(context_without_view_paths, options)).to eq("test")
end
end
end
end
end
6 changes: 0 additions & 6 deletions spec/chanko/unit_spec.rb
Expand Up @@ -245,11 +245,5 @@ def self.has_one(name, *args)
expect(ExampleModel.__example_unit_user).to eq([:class_name => "User"])
end
end

describe ".view_path" do
it "returns path for its view directory" do
expect(unit.view_path).to eq("#{Config.units_directory_path}/example_unit/views")
end
end
end
end
1 change: 1 addition & 0 deletions spec/dummy/app/views/units/example_unit
4 changes: 2 additions & 2 deletions spec/fixtures/units/example_unit/example_unit.rb
Expand Up @@ -27,7 +27,7 @@ module ExampleUnit
end

function(:render) do
render_to_string :partial => "/test", :locals => { :local => "test" }
render_to_string :partial => "/units/example_unit/test", :locals => { :local => "test" }
end

function(:nesting_locals_outer) do
Expand Down Expand Up @@ -78,7 +78,7 @@ module ExampleUnit
end

function(:render) do
render "/test", :local => "test"
render "/units/example_unit/test", :local => "test"
end

function(:blank) do
Expand Down

0 comments on commit a544457

Please sign in to comment.