Add a simple JSON API

This commit is contained in:
Petko Bordjukov 2015-10-08 02:34:19 +03:00
parent 82d44e79a7
commit bf278693b3
13 changed files with 79 additions and 0 deletions

View File

@ -0,0 +1,8 @@
class Api::EventTypesController < Api::ApplicationController
include ::CurrentConferenceAssigning
before_filter :require_current_conference!
def index
@event_types = current_conference.event_types.includes(:translations)
end
end

View File

@ -0,0 +1,8 @@
class Api::HallsController < Api::ApplicationController
include ::CurrentConferenceAssigning
before_filter :require_current_conference!
def index
@halls = current_conference.halls
end
end

View File

@ -0,0 +1,8 @@
class Api::SlotsController < Api::ApplicationController
include ::CurrentConferenceAssigning
before_filter :require_current_conference!
def index
@slots = current_conference.slots
end
end

View File

@ -0,0 +1,8 @@
class Api::SpeakersController < Api::ApplicationController
include ::CurrentConferenceAssigning
before_filter :require_current_conference!
def index
@speakers = PersonalProfile.joins(user: [:events]).where(conference: current_conference)
end
end

View File

@ -0,0 +1,8 @@
class Api::TracksController < Api::ApplicationController
include ::CurrentConferenceAssigning
before_filter :require_current_conference!
def index
@tracks = current_conference.tracks.includes(:translations)
end
end

View File

@ -15,6 +15,7 @@ class Conference < ActiveRecord::Base
has_many :volunteer_teams
has_one :call_for_participation, dependent: :destroy
has_many :participants, class_name: 'User', through: :events
has_many :slots, through: :events
accepts_nested_attributes_for :tracks, :halls, :event_types, :volunteer_teams,
reject_if: :all_blank, allow_destroy: true

View File

@ -3,6 +3,7 @@ class Event < ActiveRecord::Base
belongs_to :conference
belongs_to :track
has_one :slot
has_many :participations, dependent: :destroy
has_many :pending_participations, ->() { pending }, class_name: 'Participation'

View File

@ -0,0 +1,13 @@
json.array! @event_types do |event_type|
json.extract! event_type, :id
json.name do
event_type.translations.each do |translation|
json.set! translation.locale, translation.name
end
end
json.description do
event_type.translations.each do |translation|
json.set! translation.locale, translation.description
end
end
end

View File

@ -0,0 +1 @@
json.array! @halls, :id, :name

View File

@ -0,0 +1 @@
json.array! @slots, :hall_id, :event_id, :starts_at, :ends_at

View File

@ -0,0 +1,4 @@
json.array! @speakers do |speaker|
json.extract! speaker, :user_id, :twitter, :github, :biography, :public_email, :organisation, :last_name, :first_name
json.picture speaker.picture.serializable_hash
end

View File

@ -0,0 +1,13 @@
json.array! @tracks do |track|
json.extract! track, :id
json.name do
track.translations.each do |translation|
json.set! translation.locale, translation.name
end
end
json.description do
track.translations.each do |translation|
json.set! translation.locale, translation.description
end
end
end

View File

@ -14,6 +14,11 @@ Rails.application.routes.draw do
namespace :api do
resources :conferences, only: [] do
resources :events, only: :index
resources :speakers, only: :index
resources :tracks, only: :index
resources :event_types, only: :index
resources :halls, only: :index
resources :slots, only: :index
end
end