Skip to content

Commit

Permalink
add spec for when presenter class isn't defined but the file exists
Browse files Browse the repository at this point in the history
  • Loading branch information
szTheory committed Jan 15, 2019
1 parent a41e8db commit 19a6776
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,12 @@ MetaPresenter supports Ruby >= 2.1 and ActionPack >= 4.0. If you'd like to help
* [RDocs for the master branch](https://www.rubydoc.info/github/szTheory/meta-presenter/master)

## TODO
* fix specs for edge build
* tests for ActionMailer support
* optional `rake meta_presenter:install` that generates the scaffolding for you. Or, you can manually create the files you want.
* add support for layout-level presenters

## TODO (lower priority)
* add backwards compatibility for actionsupport3, will require enabling it in the Appraisal file and then fixing any bugs in the build
* make the presenters path configurable (instead of just app/presenters) (?)
* add Middleman support (?)

## Contributing
Expand Down
14 changes: 7 additions & 7 deletions lib/meta_presenter/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ def initialize(controller, action_name)

# @return [Class] Class constant for the built MetaPresenter::Base presenter
def presenter_class
# Try to find the class (That's not guaranteed, for example
# if the presenter class was not defined or the file wasn't found).
# Try to find the presenter class (Not guaranteed,
# for example if the presenter class wasn't defined
# or if the file wasn't found)
klass = ancestors.lazy.map do |klass_name|
presenter_class_for(klass_name)
end.find(&:itself)
Expand All @@ -53,6 +54,10 @@ def presenter_class
klass
end

def presenter_base_dir
File.join(Rails.root, "app", "presenters")
end

private
def all_ancestors
controller.class.ancestors.select do |ancestor|
Expand Down Expand Up @@ -120,10 +125,5 @@ def ancestors_until(until_class)
ancestors_list
end
end

# TODO: make the presenters path configurable
def presenter_base_dir
File.join(Rails.root, "app", "presenters")
end
end
end
20 changes: 18 additions & 2 deletions spec/meta_presenter/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,25 @@ def controller_ancestors
let(:controller_class) { ApplicationController }

it { is_expected.to be ApplicationPresenter }
end

context "controller without a presenter" do
let(:controller_class) { ApplicationController }

before do
Object.send(:remove_const, :ApplicationPresenter)
expect(Object.constants).to_not include(:ApplicationPresenter)
end

let(:presenter_file_path) { File.join(object.presenter_base_dir, "application_presenter.rb") }

# TODO: test the cases where a method for the action is/isn't defined
# (if not defined should raise NoPresenterClassDefinedError)
context "but file exists" do
before do
expect(File).to be_exists(presenter_file_path)
end

it { expect { subject }.to raise_error(MetaPresenter::Builder::FileExistsButPresenterNotDefinedError) }
end
end

context "subclass of ApplicationController" do
Expand Down

0 comments on commit 19a6776

Please sign in to comment.