GithubHelp home page GithubHelp logo

active_record_json_map's Introduction

ActiveRecordJsonMap

Installation

Add this line to your application's Gemfile:

gem 'active_record_json_map'

And then execute:

$ bundle

Or install it yourself as:

$ gem install active_record_json_map

Usage

class YourAwesomeModel < ApplicationRecord
  include ActiveRecordJsonMap

  def awesome_json *args, **hash
    # 
    {
      id:         self.id,
      title:      self.title,
      some_flag:  self.some_flag
      created_at: self.created_at.to_i
    }
  end
end
YourAwesomeModel.where(some_flag: 1).order("created_at desc").limit(3).json_map(:awesome_json)

Generated json

[
  {
    "id": 20,
    "title": "sample 3",
    "some_flag": 1,
    "created_at": 1510377530,
    "format": "YourAwesomeModel_awesome_json"
  },
  {
    "id": 19,
    "title": "sample 2",
    "some_flag": 1,
    "created_at": 1510377500,
    "format": "YourAwesomeModel_awesome_json"
  },
  {
    "id": 18,
    "title": "sample 1",
    "some_flag": 1,
    "created_at": 1510377430,
    "format": "YourAwesomeModel_awesome_json"
  },
]

Best effective use case

class Corporation < ApplicationRecord
  include ActiveRecordJsonMap

  has_many :customers

  def corporation_show_json *args, **hash
    {
      id: self.id,
      customers: self.customers.json_map(:corporation_customer_json, corporation: self)
    }
  end
end

class Customer < ApplicationRecord
  include ActiveRecordJsonMap

  belogs_to :corporation

  def corporation_customer_json *args, corporation: , **hash
    {
      id: self.id,
      title: "#{self.fullname} / #{corporation.name}"
    }
  end

  def fullname
    "his or her name"
  end
end
Corporation.limit(10).offset(0).json_map(:corporation_show_json)

Generated json

[
  {
    "id": 1,
    "customers": [
      {
        "id": 1,
        "title": "his or her name / FooCorporation Inc.",
        "format": "Customer_corporation_customer_json"
      },
      {
        "id": 2,
        "title": "his or her name / FooCorporation Inc.",
        "format": "Customer_corporation_customer_json"
      }
    ],
    "format": "Corporation_corporation_show_json"
  },
  {
    "id": 2,
    "customers": [
      {
        "id": 3,
        "title": "his or her name / BarCorporation Inc.",
        "format": "Customer_corporation_customer_json"
      },
      {
        "id": 4,
        "title": "his or her name / BarCorporation Inc.",
        "format": "Customer_corporation_customer_json"
      }
    ],
    "format": "Corporation_corporation_show_json"
  }
]

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Tatumaki/active_record_json_map.

active_record_json_map's People

Stargazers

Yuki Takemoto avatar

Watchers

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.