Skip to content

Repository files navigation

This gem is mainly targeted at Rails developers who:

  • need to execute a search based on request parameters

  • load records from a database for cached fragments

  • have a lot of view-related logic in models

It will help you to:

  • process request parameters (with simple typecasting)

  • cleanup your models and controller

  • postpone querying database until really needed (if needed at all)

  • … more …

As usual:

geminstallpresenter

To keep your code organized, create a directory “app/presenters” in your project and add this line to your environment.rb (or application.rb if you use Rails 3):

config.load_paths<<Rails.root.join("app", "presenters").to_s

Now you can have your presenters in the app/presenters directory and they’ll be loaded automatically.

Presenter:

# app/presenters/users_presenter.rbclassUsersPresenter<Presenter# following parameters will be extracted from the params hash and# properly typecasted. Nils, blanks and empty arrays will be discarded.# If a parameter's value is an array, all values in the array will# be typecastedkey:name, Stringkey:age, Integer# this creates method "users", which calls and memoizes method "find_user".presents:usersprivatedeffind_usersscope = Userscope = scope.scoped(:conditions=> { :name=>name }) iffirst_namescope = scope.scoped(:conditions=> { :age=>age }) ifagescopeendend

Controller:

# app/controllers/users_controller.rbclassUsersController<ApplicationControllerdefindex@users_presenter = UsersPresenter.new(params)
endend

View:

# app/views/users/index.html.haml
%ul
- @users_presenter.users.each do |user|
%li= user.name

If some of the code in your models is used only inside views (formatting and such), you may extract this code to a module and mix it from the presenter:

# app/presenters/user_mixin.rbmoduleUserMixindefage_ccaage<20?"too young":age>=30?"30+":ageendend

Then tell the presented to use the module:

# app/presenters/users_presenter.rb
...
presents :users, UserMixin
...

Now you can use the new method in your views:

# app/views/users/index.html.haml
%ul
- @users_presenter.users.each do |user|
%li
= user.name
= user.age_cca

These types are supported out of the box: Object, Boolean, Float, Integer, String, Time.

All of these can be nil, single value or an array of values.

It is also possible to add support for your custom types by implementing class method “typecast” for the given class. This would add support for price type:

# lib/price.rbclassPriceattr:valuedefinitialize(value)
@value = valueenddefto_sif@value"$"+@value.to_s.reverse.scan(/\d{1,3}/).join(",").reverseelse""endenddefself.typecast(value, options = {})
newvalue.to_s.scan(/\d+/).join.to_iifvalueendend

As usual:

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2010 Vladimir Bobes Tuzinsky. See LICENSE for details.

About

Presenter pattern made easy (in Rails)

Resources

Stars

5 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages