Azeroth

Build Status Code Climate Test Coverage Issue Count Gem Version Inline docs

azeroth

Yard Documentation

https://www.rubydoc.info/gems/azeroth/0.10.0

Azeroth has been designed making the coding of controllers easier as routes in controllers are usually copy, paste and replace of same code.

Azeroth was originally developed for controller actions which will respond with json or template rendering based on the requested format .json or .html where html rendering does not perform database operations

Future versions will enable html rendering to also perform database operations.

Installation

  • Install it
  gem install azeroth
  • Or add Sinclair to your Gemfile and bundle install:
  gem 'azeroth'
  bundle install azeroth

Usage

Azeroth::Resourceable

Resourceable module adds class method resource_for which adds a resource and action methods for create, show, index, update, delete, edit

It accepts options

  • only: List of actions to be built
  • except: List of actions to not to be built
  • decorator: Decorator class or flag allowing/disallowing decorators
  • before_save: Method/Proc to be ran before saving the resource on create or update
  • after_save: Method/Proc to be ran after saving the resource on create or update
  • build_with: Method/Block to be ran when building the reource on create
  • update_with: Method/Block to be ran when updating the reource on update
  • paginated: Flag when pagination should be applied
  • per_page: Number of items returned when pagination is active
  # publishers_controller.rb

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

    resource_for :publisher, only: %i[create index]
  end
  # games_controller.rb

  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
  # pokemons_controller.rb

  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

Azeroth::Decorator

Decorators are used to define how an object is exposed as json on controller responses defining which and how fields will be exposed

  # pokemon/decorator.rb

  class Pokemon::Decorator < Azeroth::Decorator
    expose :name
    expose :previous_form_name, as: :evolution_of, if: :evolution?

    def evolution?
      previous_form
    end

    def previous_form_name
      previous_form.name
    end
  end
  # pokemon/favorite_decorator.rb

  class Pokemon::FavoriteDecorator < Pokemon::Decorator
    expose :nickname
  end
  # pokemon_master/decorator.rb

  class PokemonMaster < ActiveRecord::Base
    has_one :favorite_pokemon, -> { where(favorite: true) },
            class_name: 'Pokemon'
    has_many :pokemons
  end

Exposing is done through the class method expose which accepts several options:

  • as: custom key to expose
  • if: method/block to be called checking if an attribute should or should not be exposed
  • decorator: flag to use or not a decorator or decorator class to be used