Module: Azeroth

Defined in:
lib/azeroth.rb,
lib/azeroth/model.rb,
lib/azeroth/options.rb,
lib/azeroth/version.rb,
lib/azeroth/decorator.rb,
lib/azeroth/resourceable.rb,
lib/azeroth/routes_builder.rb,
lib/azeroth/dummy_decorator.rb,
lib/azeroth/request_handler.rb,
lib/azeroth/resource_builder.rb,
lib/azeroth/decorator/options.rb,
lib/azeroth/request_handler/new.rb,
lib/azeroth/controller_interface.rb,
lib/azeroth/request_handler/edit.rb,
lib/azeroth/request_handler/show.rb,
lib/azeroth/resourceable/builder.rb,
lib/azeroth/request_handler/index.rb,
lib/azeroth/decorator/hash_builder.rb,
lib/azeroth/request_handler/create.rb,
lib/azeroth/request_handler/update.rb,
lib/azeroth/request_handler/destroy.rb,
lib/azeroth/decorator/method_builder.rb,
lib/azeroth/resourceable/class_methods.rb,
lib/azeroth/decorator/key_value_extractor.rb

Overview

Lib for easily developing controllers

Examples:

Controller without delete

class DocumentsController < ApplicationController
  include Azeroth::Resourceable

  resource_for :document, except: :delete
end

Controller with only create, show and list

class DocumentsController < ApplicationController
  include Azeroth::Resourceable

  resource_for :document, only: %w[create index show]
end

complete example gmaes and publishers

class PublishersController < ApplicationController
  include Azeroth::Resourceable
  skip_before_action :verify_authenticity_token

  resource_for :publisher, only: %i[create index]
end

class GamesController < ApplicationController
  include Azeroth::Resourceable
  skip_before_action :verify_authenticity_token

  resource_for :game, except: :delete

  private

  def games
    publisher.games
  end

  def publisher
    @publisher ||= Publisher.find(publisher_id)
  end

  def publisher_id
    params.require(:publisher_id)
  end
end

ActiveRecord::Schema.define do
  self.verbose = false

  create_table :publishers, force: true do |t|
    t.string :name
  end

  create_table :games, force: true do |t|
    t.string :name
    t.integer :publisher_id
  end
 end

class Publisher < ActiveRecord::Base
  has_many :games
end

class Game < ActiveRecord::Base
  belongs_to :publisher
end

class Game::Decorator < Azeroth::Decorator
  expose :id
  expose :name
  expose :publisher, decorator: NameDecorator
end

requesting games and publishers

post "/publishers.json", params: {
  publisher: {
    name: 'Nintendo'
  }
}

publisher = JSON.parse(response.body)
# returns
# {
#   'id' => 11,
#   'name' => 'Nintendo'
# }

publisher = Publisher.last
post "/publishers/#{publisher['id']}/games.json", params: {
  game: {
    name: 'Pokemon'
  }
}

game = Game.last

JSON.parse(response.body)
# returns
# {
#   id: game.id,
#   name: 'Pokemon',
#   publisher: {
#     name: 'Nintendo'
# }
}

Controller with before_save

class PokemonsController < ApplicationController
  include Azeroth::Resourceable

  resource_for :pokemon,
               only: %i[create update],
               before_save: :set_favorite

  private

  def set_favorite
    pokemon.favorite = true
  end

  def pokemons
    master.pokemons
  end

  def master
    @master ||= PokemonMaster.find(master_id)
  end

  def master_id
    params.require(:pokemon_master_id)
  end
end

See Also:

Defined Under Namespace

Modules: Resourceable Classes: ControllerInterface, Decorator, DummyDecorator, Model, Options, RequestHandler, ResourceBuilder, RoutesBuilder

Constant Summary collapse

VERSION =
'0.9.0'