Skip to content

Commit

Permalink
don't alter the model hash with :collection anymore. hopefully fixes t…
Browse files Browse the repository at this point in the history
…railblazer#389.

deprecate :collection_join and :method. we have a real API for that now.
  • Loading branch information
apotonick committed May 3, 2016
1 parent 43ca863 commit ada0533
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ YOU CAN PASS BLOCK BUT IT WILL BE PASSED TO Template#render.
We are hereby dropping support for `Cell::Concept` (it still works).
* Deprecating `:collection_join` and `:method` for collections.
### Awesomeness
* Introduced the concept of a context object that is being passed to all nested cells. This object is supposed to contain dependencies such as `current_user`, in Rails it contains the "parent_controller" under the `context[:controller]` key.
Expand Down
20 changes: 14 additions & 6 deletions lib/cell/collection.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
module Cell
class Collection
def initialize(ary, options, cell_class)
@method = options.delete(:method) # TODO: deprecate :method.
@join = options.delete(:collection_join)
options.delete(:collection)
@method = options.delete(:method) # TODO: remove in 5.0.
@join = options.delete(:collection_join) # TODO: remove in 5.0.

@ary = ary
@options = options
@ary = ary
@options = options
@cell_class = cell_class

deprecate_options!
end

def deprecate_options! # TODO: remove in 5.0.
warn "[Cells] The :method option is deprecated. Please use `call(method)` as documented here: http://trailblazer.to/gems/cells/api.html#collection" if @method
warn "[Cells] The :collection_join option is deprecated. Please use `join(\"<br>\")` as documented here: http://trailblazer.to/gems/cells/api.html#collection" if @collection_join
end

module Call
Expand All @@ -30,10 +38,10 @@ def join(separator="", &block)

module Layout
def call(*) # WARNING: THIS IS NOT FINAL API.
blaaaa_layout = @options.delete(:layout) # FIXME: THAT SUCKS.
layout = @options.delete(:layout) # we could also override #initialize and that there?

content = super # DISCUSS: that could come in via the pipeline argument.
ViewModel::Layout::External::Render.(content, @ary, blaaaa_layout, @options)
ViewModel::Layout::External::Render.(content, @ary, layout, @options)
end
end
include Layout
Expand Down
2 changes: 1 addition & 1 deletion lib/cell/view_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def property(*names)
# SongCell.(@song)
# SongCell.(collection: Song.all)
def call(model=nil, options={}, &block)
if model.is_a?(Hash) and array = model.delete(:collection) # FIXME.
if model.is_a?(Hash) and array = model[:collection]
return Collection.new(array, model.merge(options), self)
end

Expand Down
23 changes: 13 additions & 10 deletions test/public_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Songs < Cell::Concept
it { Cell::ViewModel.cell("public_test/song", collection: [Object, Module], collection_join: '<br/>').to_s.must_equal '[Object, {}]<br/>[Module, {}]' }

# ViewModel.cell(collection: []) passes generic options to cell.
it { Cell::ViewModel.cell("public_test/song", collection: [Object, Module], genre: 'Metal').to_s.must_equal "[Object, {:genre=>\"Metal\"}][Module, {:genre=>\"Metal\"}]" }
it { Cell::ViewModel.cell("public_test/song", collection: [Object, Module], genre: 'Metal', context: { ready: true }).to_s.must_equal "[Object, {:genre=>\"Metal\", :context=>{:ready=>true}}][Module, {:genre=>\"Metal\", :context=>{:ready=>true}}]" }

# ViewModel.cell(collection: [], method: :detail) invokes #detail instead of #show.
# TODO: remove in 5.0.
Expand All @@ -50,18 +50,21 @@ class Songs < Cell::Concept
# ViewModel.cell(collection: []).(:detail) invokes #detail instead of #show.
it { Cell::ViewModel.cell("public_test/song", collection: [Object, Module]).(:detail).must_equal '* [Object, {}]* [Module, {}]' }

# collection: [], context is passed on

# #cell(collection: [], genre: "Fusion").() doesn't change options hash.
it do
skip # don't expose #each_with_index interface.
content = ""
Cell::ViewModel.cell("public_test/song", collection: [Object, Module]).each_with_index do |cell, i|
content += (i == 1 ? cell.(:detail) : cell.())
end

content.must_equal '[Object, {}]* [Module, {}]'
Cell::ViewModel.cell("public_test/song", options = { genre: "Fusion", collection: [Object] }).()
options.to_s.must_equal "{:genre=>\"Fusion\", :collection=>[Object]}"
end

# it do
# content = ""
# Cell::ViewModel.cell("public_test/song", collection: [Object, Module]).each_with_index do |cell, i|
# content += (i == 1 ? cell.(:detail) : cell.())
# end

# content.must_equal '[Object, {}]* [Module, {}]'
# end

# cell(collection: []).join captures return value and joins it for you.
it do
Cell::ViewModel.cell("public_test/song", collection: [Object, Module]).join do |cell, i|
Expand Down

0 comments on commit ada0533

Please sign in to comment.