Skip to content

Commit

Permalink
Wiring up a backend that exposes actual content in the frontend.
Browse files Browse the repository at this point in the history
Content can be fetched for a field and a completely random piece will be returned.  The `render_details` method on the Content model can pass additional information to the frontend rendering code specific to the content type being rendered.
  • Loading branch information
bamnet committed Mar 18, 2012
1 parent 6e21c06 commit acfff44
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 15 deletions.
35 changes: 35 additions & 0 deletions app/controllers/frontend/contents_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Frontend::ContentsController < ApplicationController
layout false

before_filter :scope_setup

def scope_setup
@screen = Screen.find(params[:screen_id])
@field = Field.find(params[:field_id])
end

def index
@content = [Content.first(:offset => rand(Content.count))]
@content.each do |c|
c.pre_render(@screen, @field)
end
respond_to do |format|
format.json {
render :json => @content.to_json(
:only => [:name, :id, :duration, :type],
:methods => [:render_details]
)
}
end
end

# GET /frontend/1/fields/1/contents/1
# Trigger the render function a piece of content and passes all the params
# along for processing. Should send an inline result of the processing.
def show
@content = Content.find(params[:id])
@file = @content.render(params)
send_data @file.file_contents, :filename => @file.file_name, :type => @file.file_type, :disposition => 'inline'
end

end
6 changes: 0 additions & 6 deletions app/controllers/frontend/fields_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
class Frontend::FieldsController < ApplicationController
layout false

def contents
respond_to do |format|
format.json { render :json => [] }
end
end
end
4 changes: 4 additions & 0 deletions app/controllers/frontend/screens_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ def setup
else
# Inject the path into an fake attribute so it gets sent with the setup info.
@screen.template.path = frontend_screen_template_path(@screen, @screen.template)
@screen.template.positions.each do |p|
p.field_contents_path = frontend_screen_field_contents_path(@screen, p.field, :format => :json)
end
respond_to do |format|
format.json {
render :json => @screen.to_json(
Expand All @@ -27,6 +30,7 @@ def setup
:include => {
:positions => {
:except => [:created_at, :updated_at, :template_id],
:methods => [:field_contents_path]
},
},
:only => [:id],
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/frontend/contents_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Frontend::ContentsHelper
end
16 changes: 15 additions & 1 deletion app/models/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Content < ActiveRecord::Base
belongs_to :kind
has_many :submissions, :dependent => :destroy
has_many :feeds, :through => :submissions
has_many :media, :as => :attachable
has_many :media, :as => :attachable, :dependent => :destroy

accepts_nested_attributes_for :media
accepts_nested_attributes_for :submissions
Expand All @@ -23,6 +23,9 @@ class Content < ActiveRecord::Base
has_many :pending_feeds, :through => :submissions, :source => :feed, :conditions => "submissions.moderation_flag IS NULL"
has_many :denied_feeds, :through => :submissions, :source => :feed, :conditions => {"submissions.moderation_flag" => false}

#Magic to let us generate routes
delegate :url_helpers, :to => 'Rails.application.routes'

# Determine if content is active based on its start and end times.
# Content is active if two conditions are met:
# 1. Start date is before now, or nil.
Expand Down Expand Up @@ -52,4 +55,15 @@ def end_time=(_end_time)
end
end


# A placeholder for a pre-rendering processing trigger.
def pre_render(*arg)
true
end

# The additional data required when rendering this content.
def render_details
{:data => self.data}
end

end
14 changes: 14 additions & 0 deletions app/models/graphic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,18 @@ def render(options={})
end
end

# Placeholder attributes for rendering.
attr_accessor :screen, :field

# Store the information needed when generating the path to the image.
def pre_render(screen, field)
self.screen = screen
self.field = field
end

# Generate the path to the iamge to be displayed.
def render_details
{:path => url_helpers.frontend_screen_field_content_path(self.screen, self.field, self)}
end

end
2 changes: 2 additions & 0 deletions app/models/position.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Position < ActiveRecord::Base
validates :top, :numericality => {:greater_than_or_equal_to => -1, :less_than_or_equal_to => 1}
validates :bottom, :numericality => {:greater_than_or_equal_to => -1, :less_than_or_equal_to => 1}

attr_accessor :field_contents_path

# Compute the width of the position block.
# A Concerto-1 style attribute, figuring out
# the total width of the element.
Expand Down
4 changes: 1 addition & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
get :setup
end
resources :fields, :only => [] do
member do
get :contents, :path => 'content'
end
resources :contents, :only => [:index, :show]
end
resources :templates, :only => [:show]
end
Expand Down
18 changes: 18 additions & 0 deletions test/functional/frontend/contents_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'test_helper'

class Frontend::ContentsControllerTest < ActionController::TestCase
include Devise::TestHelpers
fixtures :screens
fixtures :fields
fixtures :contents

test "should get content for field" do
get(:index, {:screen_id => screens(:one).id, :field_id => fields(:one).id, :format => :json})
assert_response :success
assert_template false

data = ActiveSupport::JSON.decode(@response.body)
assert data.length > 0
end

end
5 changes: 0 additions & 5 deletions test/functional/frontend/fields_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,4 @@
class Frontend::FieldsControllerTest < ActionController::TestCase
include Devise::TestHelpers
fixtures :screens, :fields

test "should get content" do
get(:contents, {:screen_id => screens(:one).id, :id => fields(:one).id, :format => :json})
assert_response :success
end
end
3 changes: 3 additions & 0 deletions test/functional/frontend/screens_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class Frontend::ScreensControllerTest < ActionController::TestCase
assert_equal data['template']['positions'].length,
screens(:one).template.positions.length
assert data['template']['path'].length > 0
data['template']['positions'].each do |p|
assert p['field_contents_path'].length > 0
end
end

test "cannot setup missing screen" do
Expand Down
4 changes: 4 additions & 0 deletions test/unit/helpers/frontend/contents_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'test_helper'

class Frontend::ContentsHelperTest < ActionView::TestCase
end

0 comments on commit acfff44

Please sign in to comment.