refactor chart windows

This commit is contained in:
Lee Lawlor 2014-08-02 14:36:38 -04:00
parent b598a83e37
commit 5789de8f1d
3 changed files with 42 additions and 38 deletions

View File

@ -217,7 +217,7 @@ class ChannelsController < ApplicationController
params[:channel] = params params[:channel] = params
channel.update_attributes(channel_params) channel.update_attributes(channel_params)
channel.set_windows channel.set_windows(true)
channel.save channel.save
channel.save_tags(params[:channel][:tags]) if params[:channel][:tags].present? channel.save_tags(params[:channel][:tags]) if params[:channel][:tags].present?
channel.add_write_api_key channel.add_write_api_key

View File

@ -231,19 +231,22 @@ class Channel < ActiveRecord::Base
field5_changed? || field6_changed? || field7_changed? || field8_changed? field5_changed? || field6_changed? || field7_changed? || field8_changed?
end end
def update_chart_portlets # update the chart windows
self.fields.each do |field| def update_chart_windows
update_chart_portlet field, true # for each field
update_chart_portlet field, false self.fields.each do |field_name|
end # if the field exists, update the private and public chart window
#remove portlets for fields that don't exist if self.send("#{field_name}").present?
#iterate all chart windows... and look for a matching field update_chart_window(field_name, true)
chart_windows = windows.where(:window_type => 'chart' ) update_chart_window(field_name, false)
chart_windows.each do |window|
if self.send("field#{window.content_id}").blank?
window.destroy
end end
end end
# remove chart windows for fields that don't exist
chart_windows = windows.where(window_type: 'chart')
chart_windows.each do |chart_window|
chart_window.destroy if self.send("field#{chart_window.content_id}").blank?
end
end end
def update_status_portlet isPrivate def update_status_portlet isPrivate
@ -447,51 +450,51 @@ class Channel < ActiveRecord::Base
return new_ranking return new_ranking
end end
def set_windows # set the windows for the channel
#check for video window def set_windows(new_channel = false)
# check for video window
if video_changed? if video_changed?
update_video_portlet true update_video_portlet true
update_video_portlet false update_video_portlet false
end end
#does channel have a location and corresponding google map # does channel have a location and corresponding google map
if location_changed? if location_changed?
update_location_portlet true update_location_portlet true
update_location_portlet false update_location_portlet false
end end
#does channel have status and corresponding status window. Add the status window no matter what. Only display if it has values # does channel have status and corresponding status window. Add the status window no matter what. Only display if it has values
update_status_portlet true update_status_portlet true
update_status_portlet false update_status_portlet false
# does channel have a window for every chart element # update chart windows if this is a new channel or the fields have changed
update_chart_portlets if fields_changed? update_chart_windows if new_channel || fields_changed?
end end
private private
def update_chart_portlet (field, isPrivate) # set the chart window; field_name should be a string like 'field4'
def update_chart_window(field_name, private_flag)
field_number = field_name.last.to_i
chartWindows = windows.where(:window_type => "chart", :name => "field#{field.last.to_s}", :private_flag => isPrivate ) # get the chart window
if chartWindows.nil? || chartWindows[0].nil? window = self.windows.where(window_type: 'chart', content_id: field_number, private_flag: private_flag).first
window = Window.new
window.window_type = 'chart' # if there is no chart window for this field, add a default one
window.position = 0 if window.blank?
window.col = 0 window = Window.new(window_type: 'chart', position: 0, col: 0, title: 'window_field_chart',
window.title = "window_field_chart" name: field_name, content_id: field_number, private_flag: private_flag)
window.name = field.to_s
window.options = "&results=60&dynamic=true"
else
window = chartWindows[0]
# If there are options, use them.. if options are not available, then assign defaults
window.options ||= "&results=60&dynamic=true"
end end
window.content_id = field.last # set the options if they don't already exist
window.private_flag = isPrivate window.options ||= "&results=60&dynamic=true"
windows.push window # associate the window with the channel
window.html ="<iframe id=\"iframe#{window.id}\" width=\"450\" height=\"260\" style=\"border: 1px solid #cccccc;\" src=\"/channels/#{id}/charts/#{field.last.to_s}?width=450&height=260::OPTIONS::\" ></iframe>" self.windows.push window
# set the html
window.html = "<iframe id=\"iframe#{window.id}\" width=\"450\" height=\"260\" style=\"border: 1px solid #cccccc;\" src=\"/channels/#{self.id}/charts/#{field_number.to_s}?width=450&height=260::OPTIONS::\" ></iframe>"
# save the window, and raise an exception if it fails
if !window.save if !window.save
raise "The Window could not be saved" raise "The Window could not be saved"
end end

View File

@ -20,9 +20,10 @@ describe ChannelsController do
it "should allow a new channel to be created" do it "should allow a new channel to be created" do
post :create post :create
channel = Channel.last
response.should be_redirect response.should be_redirect
channel_id = Channel.all.last.id response.should redirect_to( channel_path(channel.id, :anchor => "channelsettings"))
response.should redirect_to( channel_path(channel_id, :anchor => "channelsettings")) channel.windows.where(window_type: 'chart').count.should eq(2)
end end
it "should allow a channel to be edited" do it "should allow a channel to be edited" do