Giter Site home page Giter Site logo

sinatra-active-model-serializers's Introduction

Codeship Status for SauloSilva/sinatra-active-model-serializers

Coverage Status

Code Climate

Sinatra::ActiveModelSerializers

Gem Version

This gem has the function of adapt the Active Model Serializers to work in Sinatra from a more practical way with models. If do you use everything at the default, just require sinatra_active_model_serializers for serializers work correctly the a json response.

Requirements

Ruby 1.9.2 or greater, Sinatra 1.4.5 or greater and Sinatra Contrib 1.4.2 or greater.

Installation

gem install sinatra-active-model-serializers

or with bundler

# Gemfile
source 'http://rubygems.org'

gem 'sinatra', '1.4.6'
gem 'sinatra-contrib', '1.4.6'
gem 'sinatra-active-record', '2.0.4'
gem 'sinatra-active-model-serializers', '0.2.0'

Configure

First you have to being with the active record configured in your system environment correctly. If the environment is not set up, see their documentation. After is simply do require from our library on your application.rb, eg.

require 'rubygems'
require 'bundler'
Bundler.require :default

class App < Sinatra::Base
  register Sinatra::ActiveRecordExtension

  get '/' do
    json Test.first
  end
end

Sets

active_model_serializers

This attribute is an object, all inserted configuration this object it will be passed on to Active Model Serializers, eg.

set :active_model_serializers, { root: false }

serializers_path

By default this attribute is set up to look for the serializers from your project in "* app / serializers *". Whether you have a different environment you can set up by inserting the path of the string, eg.

set :serializers_path, './whatever_path/serializers'

or not to automatically requires

set :serializers_path, false

Response JSON

When you return a json, you can send a the second parameter. This the second parameter is an object. This object may contain new configurations to assign to the Active Model Serializers or to rewrite any already set by default, eg.

root

get '/' do
  json Resource.first, { root: false }
end

scope

get '/' do
  json Resource.first, { scope: self }
end

Specify a serializer

If you wish to use an another serializer than the default, you can explicitly pass it through the renderer

  1. For a resource:
json Resource.first, { serializer: ResourcePreviewSerializer }
  1. For an array resource:

Use the default ArraySerializer, which will use each_serializer to serialize each element.

json @resources, { each_serializer: ResourcePreviewSerializer }

License

MIT

sinatra-active-model-serializers's People

Contributors

saulosilva avatar adrianotadao avatar

Watchers

Benjamin Fleischer avatar James Cloos 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.