Skip to content

Commit

Permalink
Merge pull request openSUSE#12 from ancorgs/master
Browse files Browse the repository at this point in the history
  • Loading branch information
hennevogel committed Jul 8, 2013
2 parents 83b1044 + d95d8f2 commit 8ea94eb
Show file tree
Hide file tree
Showing 19 changed files with 229 additions and 1 deletion.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ gem 'acts_as_commentable_with_threading'
gem 'prawn'
gem 'prawn_rails'
gem 'gravtastic'
gem 'active_model_serializers'

3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -213,6 +215,7 @@ PLATFORMS
ruby

DEPENDENCIES
active_model_serializers
acts_as_commentable_with_threading
bcrypt-ruby (~> 3.0.0)
bootstrap-sass
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/api/base_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Api::BaseController < ActionController::Base
protect_from_forgery
end
7 changes: 7 additions & 0 deletions app/controllers/api/v1/conferences_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Api::V1::ConferencesController < Api::BaseController
respond_to :json

def index
render :json => Conference.all, :serializer => ConferencesArraySerializer
end
end
11 changes: 11 additions & 0 deletions app/controllers/api/v1/events_controller.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 13 additions & 0 deletions app/controllers/api/v1/rooms_controller.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 14 additions & 0 deletions app/controllers/api/v1/speakers_controller.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 13 additions & 0 deletions app/controllers/api/v1/tracks_controller.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 26 additions & 0 deletions app/models/revision_observer.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 35 additions & 0 deletions app/serializers/conference_serializer.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 10 additions & 0 deletions app/serializers/conferences_array_serializer.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 41 additions & 0 deletions app/serializers/event_serializer.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 8 additions & 0 deletions app/serializers/room_serializer.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 17 additions & 0 deletions app/serializers/speaker_serializer.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions app/serializers/track_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class TrackSerializer < ActiveModel::Serializer
attributes :guid, :name, :color
end
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Application < Rails::Application

# 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.
Expand Down
16 changes: 16 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@
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"
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20130705055128_add_revision_to_conference.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddRevisionToConference < ActiveRecord::Migration
def change
add_column :conferences, :revision, :integer
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,6 +62,7 @@
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|
Expand Down

0 comments on commit 8ea94eb

Please sign in to comment.