Skip to content

Instantly share code, notes, and snippets.

@s0ber
Last active August 29, 2015 14:23
Show Gist options
  • Save s0ber/0f5509635bcd06d37667 to your computer and use it in GitHub Desktop.
Save s0ber/0f5509635bcd06d37667 to your computer and use it in GitHub Desktop.
BOA: front-end point of view
# When mounting an action, action will have reference to two urls: :confirm_path and :action_path.
routes.rb:
namespace: :developer ->
business_action Developer::Reject
# Renderers (`ConfirmButton` and `Modal`) can access action's :confirm_path and :action_path to render a button or a modal.
# As you see, renderers expect an action to have such methods.
actions/developers/reject.rb:
class Developer::Reject
attr_reader :confirm_path
attr_reader :action_path
initialize: (developer) ->
@developer = developer || Developer.find(@developer_id)
perform: ->
@developer.status = :rejected
@developer.save!
# we can have such convention, which will render action as a page, if it is requested via routes
as_page: ->
render_page
# or as confirmation modal
as_confirmation: ->
render_confirm_modal
# but we can also render it from anywhere, not only from requested url
render_confirm_button: ->
ConfirmButton.render(self, title: 'Reject')
private render_confirm_modal: ->
Modal.render_confirm(self, title: 'Reject Developer?')
private render_page: ->
...
# Here is an example, of how we can render an action from any view
any_view.html.slim:
.page
.title
| @developer.full_name
.fl_r
= Developer::Reject.new(@developer).render_confirm_button
# Here is how renderers will look like:
renderers/confirm_button.rb
class ConfirmButton
render: (action, options = {}) ->
h.link_to(options[:title], action.confirm_path, class: 'js-remote_modal')
renderers/modal.rb
class Modal
render_confirm: (action, options = {}) ->
options = options.reverse_merge! action_path: action.action_path
# partial accepts just pure data
= h.render('shared/confirm_modal', options: options)
@s0ber
Copy link
Author

s0ber commented Jun 30, 2015

We can even make renderers to not know about actions at all:

actions/developers/reject.rb:
class Developer::Reject

  attr_reader :confirm_path
  attr_reader :action_path

  ...

  render_confirm_button: ->
    ConfirmButton.render(title: 'Reject', confirm_path: self.confirm_path) 

Then we'll be able to use renderers not only from actions, but from anywhere. We can, for example, simplify decorators, by moving repeated parts into renderers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment