Giter Club home page Giter Club logo

daylight's Introduction

Daylight

Gem Version Build Status

Daylight extends Rails and ActiveResource to allow your client API to perform akin to ActiveRecord

Features include those like ActiveRecord such as scopes, find_by lookups, calling associations on server-side models, using through associations, and the ability to chain queries. Eases requesting complex queries on your Rails models using remoted methods.

Typical ActiveResource functionality:

API::Post.all                                      # index request
API::Post.published                                # client-based association
API::Post.find(1)                                  # show request

Daylight adds to ActiveResource with chained queries:

API::Post.where(author_id: 1)                      # simple query
API::Post.where(author_id: 1).first                # chained query
API::Post.where(author_id: 1).published            # chained query with scope
API::Post.where(author_id: 1).published.recent     # chained query with multiple scopes
API::Post.where(author_id: 1).limit(10).offset(20) # chained query with limit and offset
API::Post.where(author_id: 1).order(:date)         # chained query with ordering

API::Post.find_by(slug: '100-best-albums-2014')    # find_by lookup gets first match

Daylight can also chain queries on an ActiveResource's association. All of the chain queries above can be used to refine searches on associations:

API::Post.first.comments                           # lookup and association
API::Post.first.comments.where(user_id: 2)         # query on lookup's association
API::Post.first.comments.where(user_id: 2).first   # chained query on
API::Post.first.comments.where(user_id: 2).edited  # chained query with scope
                                                   # etc.

Daylight allows you to return collections from complex queries on your model:

API::Post.first.top_comments

Daylight packages API query details in one request when it can to lower the network overhead.

Daylight allows you to query for a record before initializing or creating it using ActiveRecord's familiar first_or_create and first_or_initialize methods.

post = API::Post.new(slug: '100-best-albums-2014')
post.author = API::User.find_or_create(username: 'reidmix')
post.save

The last query to the database uses Rails' accepts_nested_attributes_for and the User could have easily been setup with find_or_initialize to reduce the number of server-side queries.

More information can be found in the Daylight Users Guide.

Getting Started

  1. Install Daylight both on your server and your client.

     gem install daylight
    
  2. On your server, add a rails initializer:

    require 'daylight/server'
  3. On your client, setup your API:

    Daylight::API.setup!(endpoint: 'http://localhost/')
  4. Use your client models to query your API!

Development

Ah, but we have to develop our API and client models:

  1. Develop your Rails models, controllers, and routes like you do today. Add a serializer for each model in the API. Daylight provides additions to simplify adding features to your controllers and routes.

  2. Develop your client models using Daylight's extensions to ActiveResource. Daylight provides Mocks to aid in full stack testing of your client models.

  3. Consider versioning your client models and distribute them using a gem. Daylight supports versioned APIs and has facilities available for your development.

  4. More information can be found on:

License

Daylight is released under the Apache License. First released on June 30th, 2014.

daylight's People

Contributors

dmcinnes avatar marktran avatar reidmix avatar

Watchers

 avatar  avatar  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.