Sha256: f769b1ead7cac00eb49bbfc39857ddd85a30863dafbb5b45280d7eedb012edad

Contents?: true

Size: 944 Bytes

Versions: 2

Compression:

Stored size: 944 Bytes

Contents

# frozen_string_literal: true

require 'sinatra'
require 'mongoid'

Mongoid.configure do |config|
  config.clients.default = {
    uri: 'mongodb://localhost:27017/testbase'

  }
  config.belongs_to_required_by_default = false
end

class Post
  include Mongoid::Document
  field :title, type: String
  field :body, type: String
  has_many :comments
  has_one :type
end


class Type
  include Mongoid::Document
  field :name, type: String
  field :description, type: String
  belongs_to :post, optional: true
end


class Comment
  include Mongoid::Document
  field :name, type: String
  field :message, type: String
  belongs_to :post
end

class Application < Sinatra::Base
  before do
    content_type 'application/json'
  end

  get '/posts' do
    Post.all.to_json
  end

  get '/posts/:post_id' do |post_id|
    post = Post.find(post_id)
    post.attributes.merge(
      comments: post.comments,
      type: post.type
    ).to_json
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fixturizer-0.4.4 samples/app.rb
fixturizer-0.4.3 samples/app.rb