Code Monkey home page Code Monkey logo

zobi's Introduction

Zobi Logo Zobi

Zobi helps you to orchestrate your controller behaviors using the following gems :

How to use it?

Add it in your Gemfile and run bundle install:

gem 'zobi', '~> 4.0.2'

Next, include Zobi module in your controller and set modules you want to include :

extend Zobi
behaviors :inherited, :scoped, :included, :paginated, :controlled_access, :decorated

Available modules

Inherited

This module uses inherited_resources gem (for version >= 1.4.0).

This module deals with String Parameters using Parameters classes.

A Parameters inherits from Zobi::ParametersSanitizer, and should define the list of parameters and nested parameters to accept.

If your controller is namespaced, you should define the resource_type method to override its generic behavior.

If your model is namespaced, you should define the defaults resource_class for Inherited Resource, ie : defaults resource_class: ::User::Address

Here is an example :

module User
  class AddressParameters < Zobi::ParametersSanitizer

    # Optional, usefull for a namespaced controller, User::AddressesController
    # here.
    def resource_type
      :user_address
    end

    protected

    def fields
      [
        :id,
        :street,
        :zip_code,
        nested_attributes: [:id, :foo, :bar]
      ]
    end
  end
end

By default, Parameters class are discovered using the controller namespace. Given a User::AddressesController, Zobi will search for User::AddressParameters class.

If you don't want to use Parameters class, you can define the permitted_params in your controller and perform custom filtering.

Scoped

This module uses has_scope gem.

Included

This module only works with ActiveRecord because it uses the Eager Loading Associations of Active Record.

You just need to define a method named includes and add the associations to load.

def includes
  [:association]
end

Paginated

This module uses kaminari gem.

Controlled Access

This module uses pundit and devise gems.

Decorated

This module uses draper gem and has a dependency on Inherited modules for now.

By default, Zobi will try to discover the decorator class to use using the current namespece.

For example, given a controller named Admin::User::AddressesController, Zobi will try to find the appropriate decorator class in this order :

Collection :

Admin::User::AddressesDecorator
Admin::AddressesDecorator
AdressesDecorator
Admin::User::CollectionDecorator
Admin::CollectionDecorator
CollectionDecorator

Resource :

Admin::User::AddressDecorator
Admin::AddressDecorator
AdressDecorator
Admin::User::ResourceDecorator
Admin::ResourceDecorator
ResourceDecorator

If this is not the way you organize your decorators, you can override this behavior by defining a method called collection_decorator_class or decorator_class which returns the decorator class to use.

Developing

Launch test suite :

cd spec/dummy
bundle exec rake db:test:prepare
cd ../..
bundle exec rspec

Launch the dummy app :

cd spec/dummy
bundle exec rake db:migrate
bundle exec rails s

Versioning

We decided to follow rails versions to simplify update of Zobi when you want to upgrade rails.

Credits

Copyright (c) 2013 af83

Released under the MIT license

zobi's People

Contributors

klacointe avatar chatgris avatar

Watchers

Victor avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.