mirror of
https://github.com/openSUSE/osem.git
synced 2026-08-13 19:54:02 +00:00
Merge pull request #12 from ancorgs/master
JSON API for https://github.com/athanrous/suseconferenceclient
This commit is contained in:
commit
8ea94eb205
19 changed files with 229 additions and 1 deletions
1
Gemfile
1
Gemfile
|
|
@ -50,4 +50,5 @@ gem 'acts_as_commentable_with_threading'
|
|||
gem 'prawn'
|
||||
gem 'prawn_rails'
|
||||
gem 'gravtastic'
|
||||
gem 'active_model_serializers'
|
||||
|
||||
|
|
|
|||
|
|
@ -15,6 +15,8 @@ GEM
|
|||
rack-cache (~> 1.2)
|
||||
rack-test (~> 0.6.1)
|
||||
sprockets (~> 2.2.1)
|
||||
active_model_serializers (0.8.1)
|
||||
activemodel (>= 3.0)
|
||||
activemodel (3.2.11)
|
||||
activesupport (= 3.2.11)
|
||||
builder (~> 3.0.0)
|
||||
|
|
@ -213,6 +215,7 @@ PLATFORMS
|
|||
ruby
|
||||
|
||||
DEPENDENCIES
|
||||
active_model_serializers
|
||||
acts_as_commentable_with_threading
|
||||
bcrypt-ruby (~> 3.0.0)
|
||||
bootstrap-sass
|
||||
|
|
|
|||
3
app/controllers/api/base_controller.rb
Normal file
3
app/controllers/api/base_controller.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
class Api::BaseController < ActionController::Base
|
||||
protect_from_forgery
|
||||
end
|
||||
7
app/controllers/api/v1/conferences_controller.rb
Normal file
7
app/controllers/api/v1/conferences_controller.rb
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
class Api::V1::ConferencesController < Api::BaseController
|
||||
respond_to :json
|
||||
|
||||
def index
|
||||
render :json => Conference.all, :serializer => ConferencesArraySerializer
|
||||
end
|
||||
end
|
||||
11
app/controllers/api/v1/events_controller.rb
Normal file
11
app/controllers/api/v1/events_controller.rb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
class Api::V1::EventsController < Api::BaseController
|
||||
respond_to :json
|
||||
|
||||
def index
|
||||
events = Event.includes(:conference, :track, :room, :event_type, {:event_people => :person})
|
||||
unless params[:conference_id].blank?
|
||||
events = events.where("conferences.guid" => params[:conference_id])
|
||||
end
|
||||
respond_with events
|
||||
end
|
||||
end
|
||||
13
app/controllers/api/v1/rooms_controller.rb
Normal file
13
app/controllers/api/v1/rooms_controller.rb
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
class Api::V1::RoomsController < Api::BaseController
|
||||
respond_to :json
|
||||
|
||||
def index
|
||||
if params[:conference_id].blank?
|
||||
rooms = Room.all
|
||||
else
|
||||
conference = Conference.find_by_guid(params[:conference_id])
|
||||
rooms = conference.rooms
|
||||
end
|
||||
respond_with rooms
|
||||
end
|
||||
end
|
||||
14
app/controllers/api/v1/speakers_controller.rb
Normal file
14
app/controllers/api/v1/speakers_controller.rb
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
class Api::V1::SpeakersController < Api::BaseController
|
||||
respond_to :json
|
||||
|
||||
def index
|
||||
if params[:conference_id].blank?
|
||||
people = Person.joins(:event_people)
|
||||
else
|
||||
people = Person.joins(:event_people => {:event => :conference})
|
||||
people = people.where("conferences.guid" => params[:conference_id])
|
||||
end
|
||||
people = people.where("event_people.event_role" => "speaker")
|
||||
render :json => people, :each_serializer => SpeakerSerializer
|
||||
end
|
||||
end
|
||||
13
app/controllers/api/v1/tracks_controller.rb
Normal file
13
app/controllers/api/v1/tracks_controller.rb
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
class Api::V1::TracksController < Api::BaseController
|
||||
respond_to :json
|
||||
|
||||
def index
|
||||
if params[:conference_id].blank?
|
||||
tracks = Track.all
|
||||
else
|
||||
tracks = Track.joins(:conference)
|
||||
tracks = tracks.where("conferences.guid" => params[:conference_id])
|
||||
end
|
||||
respond_with tracks
|
||||
end
|
||||
end
|
||||
26
app/models/revision_observer.rb
Normal file
26
app/models/revision_observer.rb
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#
|
||||
# suseconferenceclient relies on a 'revision' attribute for caching and
|
||||
# doing some calculations.
|
||||
#
|
||||
# It should be incremented after any change in the conference or in any
|
||||
# associated models
|
||||
#
|
||||
# This observer updates the revision column in a non-intrusive way,
|
||||
# preventing validations, callbacks or exceptions to be triggered
|
||||
#
|
||||
# Relying on paper_trail could also be an option, but a 'revision' column
|
||||
# in table 'conferences' looks like a more simple and straightforward solution
|
||||
#
|
||||
class RevisionObserver < ActiveRecord::Observer
|
||||
observe :conference, :event, :room, :social_event, :track
|
||||
|
||||
def after_save(model)
|
||||
begin
|
||||
conference = model.kind_of?(Conference) ? model : model.conference
|
||||
conference.reload.increment(:revision)
|
||||
conference.update_column(:revision, conference.revision)
|
||||
rescue
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
35
app/serializers/conference_serializer.rb
Normal file
35
app/serializers/conference_serializer.rb
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
class ConferenceSerializer < ActiveModel::Serializer
|
||||
attributes :guid, :name, :description, :year, :socialtag, :date_range, :url, :revision
|
||||
|
||||
def name
|
||||
object.title
|
||||
end
|
||||
|
||||
def year
|
||||
object.start_date.try(:year)
|
||||
end
|
||||
|
||||
def socialtag
|
||||
object.social_tag
|
||||
end
|
||||
|
||||
def revision
|
||||
object.revision || 0
|
||||
end
|
||||
|
||||
# FIXME: adjusting the format the DIRTY way, for oSC13.
|
||||
# If you think this is ugly, don't look at the methods below
|
||||
def date_range
|
||||
object.date_range_string.try(:split, ",").try(:first)
|
||||
end
|
||||
|
||||
# FIXME: just giving suseconferenceclient something to play with
|
||||
def description
|
||||
"openSUSE Conference 2013 - Power to the Geeko"
|
||||
end
|
||||
|
||||
# FIXME: same than the former
|
||||
def url
|
||||
"https://conference.opensuse.org/"
|
||||
end
|
||||
end
|
||||
10
app/serializers/conferences_array_serializer.rb
Normal file
10
app/serializers/conferences_array_serializer.rb
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#
|
||||
# Needed in order to add the API version number to the conferences array
|
||||
#
|
||||
class ConferencesArraySerializer < ActiveModel::ArraySerializer
|
||||
|
||||
def as_json(*args)
|
||||
json = super
|
||||
json.merge!(:version => 1)
|
||||
end
|
||||
end
|
||||
41
app/serializers/event_serializer.rb
Normal file
41
app/serializers/event_serializer.rb
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
class EventSerializer < ActiveModel::Serializer
|
||||
include ActionView::Helpers::TextHelper
|
||||
|
||||
attributes :guid, :title, :length, :date, :language, :abstract,
|
||||
:speaker_ids, :type, :room, :track
|
||||
|
||||
def date
|
||||
object.start_time
|
||||
end
|
||||
|
||||
def speaker_ids
|
||||
speakers = object.event_people.select {|i| i.event_role == "speaker" }
|
||||
speakers.map {|i| i.person.guid}
|
||||
end
|
||||
|
||||
def type
|
||||
object.event_type.try(:title)
|
||||
end
|
||||
|
||||
def room
|
||||
object.room.try(:guid)
|
||||
end
|
||||
|
||||
def track
|
||||
object.track.try(:guid)
|
||||
end
|
||||
|
||||
def abstract
|
||||
# This should never happen
|
||||
if object.abstract.blank?
|
||||
nil
|
||||
else
|
||||
simple_format(object.abstract).gsub("\n", "")
|
||||
end
|
||||
end
|
||||
|
||||
# FIXME: duplicated logic from Event#as_json
|
||||
def length
|
||||
object.event_type.try(:length) || 25
|
||||
end
|
||||
end
|
||||
8
app/serializers/room_serializer.rb
Normal file
8
app/serializers/room_serializer.rb
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
class RoomSerializer < ActiveModel::Serializer
|
||||
attributes :guid, :name, :description
|
||||
|
||||
# FIXME: just giving suseconferenceclient something to play with
|
||||
def description
|
||||
""
|
||||
end
|
||||
end
|
||||
17
app/serializers/speaker_serializer.rb
Normal file
17
app/serializers/speaker_serializer.rb
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
class SpeakerSerializer < ActiveModel::Serializer
|
||||
include ActionView::Helpers::TextHelper
|
||||
|
||||
attributes :guid, :name, :company, :biography
|
||||
|
||||
def name
|
||||
object.public_name
|
||||
end
|
||||
|
||||
def biography
|
||||
if object.biography.blank?
|
||||
nil
|
||||
else
|
||||
simple_format(object.biography).gsub("\n", "")
|
||||
end
|
||||
end
|
||||
end
|
||||
3
app/serializers/track_serializer.rb
Normal file
3
app/serializers/track_serializer.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
class TrackSerializer < ActiveModel::Serializer
|
||||
attributes :guid, :name, :color
|
||||
end
|
||||
|
|
@ -25,6 +25,7 @@ module Osem
|
|||
|
||||
# Activate observers that should always be running.
|
||||
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
|
||||
config.active_record.observers = :revision_observer
|
||||
|
||||
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
|
||||
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
|
||||
|
|
|
|||
|
|
@ -53,6 +53,22 @@ Osem::Application.routes.draw do
|
|||
delete "/register" => "ConferenceRegistration#unregister"
|
||||
end
|
||||
end
|
||||
|
||||
namespace :api, defaults: {format: 'json'} do
|
||||
namespace :v1 do
|
||||
resources :conferences, :only => :index do
|
||||
resources :rooms, :only => :index
|
||||
resources :tracks, :only => :index
|
||||
resources :speakers, :only => :index
|
||||
resources :events, :only => :index
|
||||
end
|
||||
resources :rooms, :only => :index
|
||||
resources :tracks, :only => :index
|
||||
resources :speakers, :only => :index
|
||||
resources :events, :only => :index
|
||||
end
|
||||
end
|
||||
|
||||
match "/admin" => redirect("/admin/conference")
|
||||
|
||||
root :to => "home#index"
|
||||
|
|
|
|||
5
db/migrate/20130705055128_add_revision_to_conference.rb
Normal file
5
db/migrate/20130705055128_add_revision_to_conference.rb
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
class AddRevisionToConference < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :conferences, :revision, :integer
|
||||
end
|
||||
end
|
||||
|
|
@ -11,7 +11,7 @@
|
|||
#
|
||||
# It's strongly recommended to check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(:version => 20130626095459) do
|
||||
ActiveRecord::Schema.define(:version => 20130705055128) do
|
||||
|
||||
create_table "call_for_papers", :force => true do |t|
|
||||
t.date "start_date", :null => false
|
||||
|
|
@ -62,6 +62,7 @@ ActiveRecord::Schema.define(:version => 20130626095459) do
|
|||
t.datetime "logo_updated_at"
|
||||
t.boolean "use_dietary_choices", :default => false
|
||||
t.boolean "use_supporter_levels", :default => false
|
||||
t.integer "revision"
|
||||
end
|
||||
|
||||
create_table "dietary_choices", :force => true do |t|
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue