Skip to content

Commit

Permalink
fix specs
Browse files Browse the repository at this point in the history
  • Loading branch information
szTheory committed Jan 15, 2019
1 parent f8e5ae1 commit a41e8db
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,15 @@ 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 again
* add Middleman support
* add support for actionsupport3, will require enabling it in the Appraisal file and then fixing any bugs in the build
* 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 layouts
* 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
29 changes: 23 additions & 6 deletions lib/meta_presenter/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,33 @@ def initialize(presenter_class_name, presenter_file_path)
end
end

class NoPresenterClassDefinedError < NameError
# Create a new error
def initialize(controller, action_name)
super("No presenter defined for #{controller.class.to_s}##{action_name}")
end
end

# @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).
klass_name = ancestors.find do |klass_name|
klass = ancestors.lazy.map do |klass_name|
presenter_class_for(klass_name)
end.find(&:itself)

if klass.nil?
raise NoPresenterClassDefinedError.new(controller, action_name)
end

# Return the actual class
presenter_class_for(klass_name)
klass
end

private
def all_ancestors
controller.class.ancestors
controller.class.ancestors.select do |ancestor|
ancestor <= ActionController::Base
end
end

def mailer_ancestors
Expand All @@ -68,11 +80,11 @@ def presenter_class_for(klass_name)
# No corresponding presenter class was found
rescue NameError => e
filename = "#{presenter_class_name.underscore}.rb"
presenter_file_path = File.join(Rails.root, "app", "presenters", filename)
presenter_file_path = File.join(presenter_base_dir, filename)
if File.exists?(presenter_file_path)
raise FileExistsButPresenterNotDefinedError.new(presenter_class_name, presenter_file_path)
else
return :undefined_presenter_class
return nil
end
end
end
Expand Down Expand Up @@ -108,5 +120,10 @@ 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
7 changes: 7 additions & 0 deletions spec/meta_presenter/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,18 @@ def controller_ancestors
let(:controller_class) { ApplicationController }

it { is_expected.to be ApplicationPresenter }

# TODO: test the cases where a method for the action is/isn't defined
# (if not defined should raise NoPresenterClassDefinedError)
end

context "subclass of ApplicationController" do
let(:controller_class) { PagesController }

before do
expect(controller_class.ancestors).to include(ApplicationController)
end

context "action with a presenter class defined" do
let(:action_name) { "logs" }
before { expect(Object).to_not be_const_defined('LogsPresenter') }
Expand Down

0 comments on commit a41e8db

Please sign in to comment.