Code Monkey home page Code Monkey logo

roar-rails's Introduction

roar-rails

Makes using Roar's representers in your Rails app fun.

Features

  • Rendering with responders
  • URL helpers in representers
  • Better tests
  • Autoloading

Rendering with #respond_with

Easily render resources using representers with the built-in responder.

class SingersController < ApplicationController
  respond_to :json

  def show
    singer = Singer.find_by_id(params[:id])
    respond_with singer
  end

  def self.responder
    Class.new(super).send :include, Roar::Rails::Responder
  end

end

Need to use a representer with a different name than your model? Pass it in using the :with_representer option:

class SingersController < ApplicationController
  respond_to :json

  def show
    singer = Musician.find_by_id(params[:id])
    respond_with singer, :with_representer => SingerRepresenter
  end

  def self.responder
    Class.new(super).send :include, Roar::Rails::Responder
  end

end

Goes great with Jose Valim's responders gem!

class SingersController < ApplicationController
  respond_to :json

  responders Roar::Rails::Responder

  def show
    singer = Singer.find_by_id(params[:id])
    respond_with singer
  end

end

URL Helpers

Any URL helpers from the Rails app are automatically available in representers.

module FruitRepresenter
  include Roar::Representer::JSON

  link :self do
    fruit_url self
  end

To get the hyperlinks up and running, please make sure to set the right host name in your environment files (config/environments):

config.representer.default_url_options = {:host => "127.0.0.1:3000"}

Testing

Autoloading

Put your representers in app/representers and they will be autoloaded by Rails. Also, frequently used modules as media representers and features don't need to be required manually.

Contributors

  • Railslove and especially Michael Bumann [bumi] have heavily supported development of roar-rails ("resource :singers").

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.