refactor channel rankings

This commit is contained in:
Lee Lawlor 2014-08-01 15:23:28 -04:00
parent a4daedbfbc
commit acd42142dc
34 changed files with 273 additions and 35 deletions

View File

@ -260,11 +260,7 @@ class ApplicationController < ActionController::Base
def get_channel_data def get_channel_data
@channel = current_user.channels.find(params[:channel_id]) if params[:channel_id] @channel = current_user.channels.find(params[:channel_id]) if params[:channel_id]
@channel = current_user.channels.find(params[:id]) if @channel.nil? and params[:id] @channel = current_user.channels.find(params[:id]) if @channel.nil? and params[:id]
@channel.ranking = @channel.set_ranking if @channel.ranking.blank?
if @channel.ranking.blank?
@channel.ranking = @channel.calc_ranking
end
@key = @channel.api_keys.write_keys.first.try(:api_key) || "" @key = @channel.api_keys.write_keys.first.try(:api_key) || ""
end end

View File

@ -177,7 +177,6 @@ class ChannelsController < ApplicationController
get_channel_data get_channel_data
end end
def update def update
# get the current user or find the user via their api key # get the current user or find the user via their api key
@user = current_user || User.find_by_api_key(get_apikey) @user = current_user || User.find_by_api_key(get_apikey)
@ -195,6 +194,7 @@ class ChannelsController < ApplicationController
@channel.assign_attributes(channel_params) @channel.assign_attributes(channel_params)
@channel.set_windows @channel.set_windows
@channel.save @channel.save
@channel.set_ranking
else else
session[:errors] = @channel.errors session[:errors] = @channel.errors
redirect_to channel_path(@channel.id, :anchor => "channelsettings") and return redirect_to channel_path(@channel.id, :anchor => "channelsettings") and return
@ -221,6 +221,7 @@ class ChannelsController < ApplicationController
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
channel.set_ranking
@channel_id = channel.id @channel_id = channel.id
respond_to do |format| respond_to do |format|
format.json { render :json => channel.to_json(Channel.private_options) } format.json { render :json => channel.to_json(Channel.private_options) }

View File

@ -68,7 +68,6 @@ class Channel < ActiveRecord::Base
attr_readonly :created_at attr_readonly :created_at
after_commit :set_default_name after_commit :set_default_name
after_commit :set_ranking, :unless => "ranking == calc_ranking"
before_destroy :delete_feeds before_destroy :delete_feeds
@ -221,14 +220,10 @@ class Channel < ActiveRecord::Base
def public?; self.public_flag; end def public?; self.public_flag; end
# check if the video has changed # check if the video has changed
def video_changed? def video_changed?; video_id_changed? || video_type_changed?; end
video_id_changed? || video_type_changed?
end
# check if the location has changed # check if the location has changed
def location_changed? def location_changed?; latitude_changed? || longitude_changed?; end
latitude_changed? || longitude_changed?
end
# check if the any of the fields have changed # check if the any of the fields have changed
def fields_changed? def fields_changed?
@ -437,16 +432,19 @@ class Channel < ActiveRecord::Base
} }
end end
def calc_ranking # set the ranking correctly for the channel
result = 0 def set_ranking
result = result + 15 unless name.blank? new_ranking = 0
result = result + 20 unless description.blank? new_ranking += 15 if name.present?
result = result + 15 unless latitude.blank? || longitude.blank? new_ranking += 20 if description.present?
result = result + 15 unless url.blank? new_ranking += 15 if latitude.present? && longitude.present?
result = result + 15 unless video_id.blank? || video_type.blank? new_ranking += 15 if url.present?
new_ranking += 15 if video_id.present? && video_type.present?
new_ranking += 20 if tags.present?
result = result + 20 unless tags.empty? # update the ranking if it has changed
result update_attribute(:ranking, new_ranking) if self.ranking != new_ranking
return new_ranking
end end
def set_windows def set_windows
@ -472,10 +470,6 @@ class Channel < ActiveRecord::Base
private private
def set_ranking
update_attribute(:ranking, calc_ranking) unless ranking == calc_ranking
end
def update_chart_portlet (field, isPrivate) def update_chart_portlet (field, isPrivate)
chartWindows = windows.where(:window_type => "chart", :name => "field#{field.last.to_s}", :private_flag => isPrivate ) chartWindows = windows.where(:window_type => "chart", :name => "field#{field.last.to_s}", :private_flag => isPrivate )
@ -503,13 +497,10 @@ class Channel < ActiveRecord::Base
end end
end end
# set the default channel name
def set_default_name def set_default_name
update_attribute(:name, "#{I18n.t(:channel_default_name)} #{self.id}") if self.name.blank? update_attribute(:name, "#{I18n.t(:channel_default_name)} #{self.id}") if self.name.blank?
end end
end end

View File

@ -1,7 +1,6 @@
<div> <div>
<% @channels.each do |channel| %> <% @channels.each do |channel| %>
<% channel.ranking = channel.calc_ranking if channel.ranking.blank? %>
<div class="public_channel_box"> <div class="public_channel_box">
<div class="public_channel_inner"> <div class="public_channel_inner">
<p class="public_channel_name"> <p class="public_channel_name">

View File

@ -0,0 +1,8 @@
class SetRankingsForChannels < ActiveRecord::Migration
def change
Channel.find_each do |channel|
channel.set_ranking
end
end
end

View File

@ -0,0 +1,7 @@
class ModifyRankingIndexOnChannels < ActiveRecord::Migration
def change
remove_index :channels, :ranking
add_index :channels, [:ranking, :updated_at]
end
end

View File

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20140722231849) do ActiveRecord::Schema.define(version: 20140801191621) do
create_table "active_admin_comments", force: true do |t| create_table "active_admin_comments", force: true do |t|
t.string "namespace" t.string "namespace"
@ -108,7 +108,7 @@ ActiveRecord::Schema.define(version: 20140722231849) do
end end
add_index "channels", ["public_flag", "last_entry_id", "updated_at"], name: "channels_public_viewable", using: :btree add_index "channels", ["public_flag", "last_entry_id", "updated_at"], name: "channels_public_viewable", using: :btree
add_index "channels", ["ranking"], name: "index_channels_on_ranking", using: :btree add_index "channels", ["ranking", "updated_at"], name: "index_channels_on_ranking_and_updated_at", using: :btree
add_index "channels", ["realtime_io_serial_number"], name: "index_channels_on_realtime_io_serial_number", using: :btree add_index "channels", ["realtime_io_serial_number"], name: "index_channels_on_realtime_io_serial_number", using: :btree
add_index "channels", ["slug"], name: "index_channels_on_slug", using: :btree add_index "channels", ["slug"], name: "index_channels_on_slug", using: :btree
add_index "channels", ["user_id"], name: "index_channels_on_user_id", using: :btree add_index "channels", ["user_id"], name: "index_channels_on_user_id", using: :btree
@ -378,6 +378,7 @@ ActiveRecord::Schema.define(version: 20140722231849) do
t.datetime "remember_created_at" t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false t.integer "sign_in_count", default: 0, null: false
t.string "authentication_token" t.string "authentication_token"
t.datetime "terms_agreed_at"
end end
add_index "users", ["api_key"], name: "index_users_on_api_key", using: :btree add_index "users", ["api_key"], name: "index_users_on_api_key", using: :btree

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

Binary file not shown.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

Binary file not shown.

File diff suppressed because one or more lines are too long

View File

@ -135,6 +135,7 @@ describe ChannelsController do
end end
it 'returns JSON' do it 'returns JSON' do
post :create, {:key => @user.api_key, :name => 'mychannel', :format => 'json'} post :create, {:key => @user.api_key, :name => 'mychannel', :format => 'json'}
Channel.last.ranking.should_not be_blank
JSON.parse(response.body)['name'].should eq("mychannel") JSON.parse(response.body)['name'].should eq("mychannel")
end end
it 'returns XML' do it 'returns XML' do
@ -154,6 +155,7 @@ describe ChannelsController do
end end
it 'returns JSON' do it 'returns JSON' do
post :update, {:id => @channel.id, :key => @user.api_key, :name => 'newname', :format => 'json'} post :update, {:id => @channel.id, :key => @user.api_key, :name => 'newname', :format => 'json'}
Channel.last.ranking.should_not be_blank
JSON.parse(response.body)['name'].should eq("newname") JSON.parse(response.body)['name'].should eq("newname")
end end
it 'returns XML' do it 'returns XML' do

View File

@ -59,6 +59,13 @@ describe Channel do
channel.should be_valid channel.should be_valid
end end
it "should set ranking correctly" do
channel = Channel.create
channel.set_ranking.should eq(15)
channel.description = "foo"
channel.set_ranking.should eq(35)
end
it "should accept utf8" do it "should accept utf8" do
channel = Channel.create(:name => "ǎ") channel = Channel.create(:name => "ǎ")
channel.reload channel.reload
@ -128,3 +135,4 @@ describe Channel do
end end
end end
end end