Sha256: 03f8f6fddf27731debe97c768feb363235acbc3fd74c560d4401eb1f82a4f220

Contents?: true

Size: 1.93 KB

Versions: 3

Compression:

Stored size: 1.93 KB

Contents

# Models a controller, including routes and views. Little more than a container
# class for the Attribute element.

require 'katapult/element'
require 'katapult/action'
require 'generators/katapult/w_u_i/w_u_i_generator'

module Katapult
  class WUI < Element

    options :model
    attr_accessor :actions

    RAILS_ACTIONS = %w[ index show new create edit update destroy ]
    UnknownActionError = Class.new(StandardError)
    UnknownModelError = Class.new(StandardError)

    def initialize(*args)
      self.actions = []

      super
    end

    # DSL
    def action(name, options = {})
      actions << Action.new(:new, options) if name.to_s == 'create'
      actions << Action.new(:edit, options) if name.to_s == 'update'

      actions << Action.new(name, options)
    end

    # DSL
    def crud
      %i(index show create update destroy).each &method(:action)
    end

    def model
      model_name = @model || self.name
      application_model.get_model(model_name) or raise UnknownModelError,
        "Could not find a model named #{model_name}"
    end

    def custom_actions
      actions.reject { |a| RAILS_ACTIONS.include? a.name }
    end

    def find_action(action_name)
      actions.find { |a| a.name == action_name.to_s }
    end

    def path(action, object_name = nil)
      unless action.is_a?(Action)
        not_found_message = "Unknown action '#{action}'"
        action = find_action(action) or raise UnknownActionError, not_found_message
      end

      member_path = "#{model.name(:variable)}_path"
      collection_path = "#{model.name(:variables)}_path"

      path = ''
      path << action.name << '_' unless %w[index show destroy].include?(action.name)
      path << (action.member? ? member_path : collection_path)
      path << "(#{object_name})" if object_name

      path
    end

    def model_name(kind = nil)
      model.name(kind)
    end

    def render
      Generators::WUIGenerator.new(self).invoke_all
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
katapult-0.1.2 lib/katapult/wui.rb
katapult-0.1.1 lib/katapult/wui.rb
katapult-0.1.0 lib/katapult/wui.rb