cookpad/chanko

View on GitHub
lib/generators/chanko/unit/templates/unit.rb.erb

Summary

Maintainability
Test Coverage
module <%= class_name %>
  include Chanko::Unit

  # ## active_if
  # This block is used to decide if this unit is active or not.
  # `context` is the receiver object of `invoke`.
  # `options` is passed via `invoke(:foo, :bar, :active_if_options => { ... })`.
  # By default, this is set as `active_if { true }`.
  #
  # ```
  # active_if do |context, options|
  #   true
  # end
  # ```

  # ## raise_error
  # `raise_error` is used to force an unit to raise up errors occured in invoking.
  # You can force to raise up errors also by `Config.raise_error`.
  #
  # ```
  # raise_error
  # ```

  # ## function
  # In controller or view context, you can call functions defined by `function`
  # via `invoke(:<%= file_name %>, :function_name)`.
  #
  # ```
  # scope(:controller) do
  #   function(:function_name) do
  #     "Chanko!"
  #   end
  # end
  # ```

  # ## render
  # If you want to place the views path under the unit, create a symbolic link under app/views/units and refer to it.
  # ```
  # scope(:view) do
  #   function(:function_name) do
  #     render "/units/<%= your_unit_name.underscore %>/example", :foo => "bar"
  #   end
  # end
  # ```

  # ## models
  # In models block, you can expand model features by `expand` method.
  # The expanded methods are available via unit proxy like `User.unit.active`,
  # `User.find(params[:id]).unit.active?` or `User.unit.gc_all_soft_deleted_users`.
  #
  # ```
  # models do
  #   expand(:User) do
  #     scope :active, lambda { where(:deleted_at => nil) }
  #
  #     def active?
  #       deleted_at.nil?
  #     end
  #
  #     class_methods do
  #       def gc_all_soft_deleted_users
  #         where.not(deleted_at: nil).delete_all
  #       end
  #     end
  #   end
  # end
  # ```

  # ## shared
  # You can call methods defined by `shared` in invoking.
  #
  # ```
  # shared(:hello) do |world|
  #   "Hello, #{world}"
  # end
  # ```

  # ## helpers
  # You can call helpers in view via unit proxy like `unit.helper_method`
  #
  # ```
  # helpers do
  #   def helper_method
  #     "helper method"
  #   end
  # end
  # ```
end