Skip to content

Commit

Permalink
Chanko no longer prepares view_paths
Browse files Browse the repository at this point in the history
  • Loading branch information
eudoxa committed Oct 7, 2023
1 parent 8f3fbf2 commit 24df553
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 71 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 its previous version, Chanko added the views path of the unit to Rails' view file search. However, it no longer does that. If you still want to place the views path under the unit, please create a symbolic link under app/views/units and refer to 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
23 changes: 0 additions & 23 deletions spec/chanko/function_spec.rb
Expand Up @@ -50,16 +50,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 +58,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 24df553

Please sign in to comment.