Project

azeroth

0.0
Low commit activity in last 3 years
A long-lived project that still receives updates
Azeroth
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.4.8
= 0.14.1
= 1.0.0
= 13.0.6
= 6.0.3
= 3.11.0
= 3.11.0
= 0.80.1
= 1.4.2
= 0.9.9
= 7.0.4.3
= 7.0.4.3
= 1.13.8
= 7.0.4.3
= 4.7.0
= 0.9.27
= 5.16.2
= 3.11.1
= 0.21.2
~> 1.2022.1

Runtime

>= 0.1.1
>= 2.0.0
 Project Readme

Azeroth

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

azeroth

Yard Documentation

https://www.rubydoc.info/gems/azeroth/1.0.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
  class PaginatedDocumentsController < ApplicationController
    include Azeroth::Resourceable

    resource_for :document, only: 'index', paginated: true
  end

  30.times { create(:document) }

  get '/paginated_documents.json'

  # returns Array with 20 first documents
  # returns in the headers pagination headers
  # {
  #   'pages' => 2,
  #   'per_page' => 20,
  #   'page' => 1
  # }

  get '/paginated_documents.json?page=2'

  # returns Array with 10 next documents
  # returns in the headers pagination headers
  # {
  #   'pages' => 2,
  #   'per_page' => 20,
  #   'page' => 2
  # }

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