Action Conductor
DRY-up Rails Controllers with conductors
Installation
Add this line to your application's Gemfile:
gem 'action_conductor'
And then execute:
$ bundle
Usage
Define a Conductor in app/conductors
class PagesConductor < ActionConductor::Base
# pass it a value directly
export :page, "Hello World"
# delegate to a block for compilation
export :meta do
"some computed value"
end
# optionally pass arguments from the computed action to the computed value
export :foo do |args|
"some computed value with arguments"
end
# blocks are executed in the context of the conductor instance, so...
export :bar do
bar_conductor
end
# ... delegates to ...
def bar_conductor
end
end
Bind it to a Rails Controller
class PagesController < ApplicationController
conductor :pages
# ...
end
Export computed values via exports
class PagesController < ApplicationController
# ...
def show
@page = exports
end
end
or export multiple computed values
class PagesController < ApplicationController
# ...
def show
@page, @meta = exports # => exports in the established order
end
end
explicitly declare which exports to export, and in which order
class PagesController < ApplicationController
# ...
def show
@meta, @page = exports(:meta, :page)
end
end
Pass arguments to the export block
class PagesController < ApplicationController
# ...
def show
@address = exports(:address, "111 Main St.")
end
end
class PagesConductor < ActionConductor::Base
export :address do |street|
"#{street} Medford, OR 97501"
end
end
In case there is a competing exports
method on the controller, you can access it through
the conductor
instance
class PagesController < ApplicationController
# ...
def show
@foo = conductor.exports(:foo)
end
end
Contributing
- Fork it ( https://github.com/[my-github-username]/conductor/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request