Skip to content

Commit 4d4268e

Browse files
committed
Allow FieldConfigs to change shuffle algorithm.
1 parent b497025 commit 4d4268e

File tree

5 files changed

+31
-35
lines changed

5 files changed

+31
-35
lines changed

app/controllers/frontend/contents_controller.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ class Frontend::ContentsController < ApplicationController
44
before_filter :scope_setup
55
before_filter :screen_api
66

7+
DEFAULT_SHUFFLE = 'WeightedShuffle'
8+
79
def scope_setup
810
@screen = Screen.find(params[:screen_id])
911
@field = Field.find(params[:field_id])
@@ -12,7 +14,8 @@ def scope_setup
1214

1315
def index
1416
require 'frontend_content_order'
15-
shuffler_klass = FrontendContentOrder.load_shuffler('WeightedShuffle')
17+
shuffle_config = FieldConfig.get(@screen, @field, 'shuffler') || DEFAULT_SHUFFLE
18+
shuffler_klass = FrontendContentOrder.load_shuffler(shuffle_config)
1619
session_key = "frontend_#{@screen.id}_#{@field.id}".to_sym
1720
shuffler = shuffler_klass.new(@screen, @field, @subscriptions, session[session_key])
1821
@content = shuffler.next_contents()

app/models/field_config.rb

+8-29
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,13 @@ class FieldConfig < ActiveRecord::Base
77
validates_presence_of :key, :field_id, :screen_id
88
validates_uniqueness_of :key, :scope => [:screen_id, :field_id]
99

10-
# Enable hash-like access to table for ease of use
11-
# Shortcut for self.get(key)
12-
def self.[](key)
13-
self.get(key.to_s)
14-
end
15-
16-
# Make setting values from Rails nice and easy
17-
def self.set(key,value)
18-
#only set this if the config already exists
19-
setting = FieldConfig.where(:key => key).first
20-
setting.update_column(:value, value)
21-
end
22-
23-
# Make getting values from Rails nice and easy
24-
# Returns false if key isn't found or the config is broken.
25-
def self.get(key)
26-
setting = FieldConfig.where(:key => key).first
27-
if setting.nil?
28-
raise "Field config key #{key} not found!"
29-
end
30-
return setting.value
31-
end
32-
33-
# Creates a Field Config entry by taking the key and value desired
34-
def self.make_field_config(field_id, config_key,config_value)
35-
# first_or_create: check whether first returns nil or not; if it does return nil, create is called
36-
FieldConfig.where(:key => config_key).first_or_create(:field_id => field_id, :key => config_key, :value => config_value)
37-
end
38-
10+
def self.get(screen, field, key)
11+
field_config = FieldConfig.where(:screen_id => screen.id, :field_id => field.id, :key => key).first
12+
if !field_config.nil?
13+
return field_config.value
14+
else
15+
return nil
16+
end
17+
end
3918
end
4019

lib/frontend_content_order.rb

+7-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ module FrontendContentOrder
99
# Defaults to a BaseShuffle algorithm.
1010
# @return [Class] Shuffle algorithm class.
1111
def self.load_shuffler(shuffler_name='BaseShuffle')
12-
require shuffler_name.underscore unless defined? shuffler_name.constantize
13-
return shuffler_name.constantize
12+
begin
13+
require shuffler_name.underscore unless defined? shuffler_name.constantize
14+
return shuffler_name.constantize
15+
rescue
16+
Rails.logger.error("Trouble loading shuffler: #{shuffler_name}.")
17+
return BaseShuffle
18+
end
1419
end
1520
end

test/fixtures/field_configs.yml

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
# model remove the '{}' from the fixture names and add the columns immediately
55
# below each fixture, per the syntax in the comments below
66
#
7-
one: {}
8-
# column: value
9-
#
7+
one:
8+
screen: one
9+
field: one
10+
key: keyname
11+
value: valuehere
12+
13+
1014
two: {}
1115
# column: value

test/unit/field_config_test.rb

+5
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ class FieldConfigTest < ActiveSupport::TestCase
2323
dup.screen = screens(:two)
2424
assert dup.valid?
2525
end
26+
27+
test "fieldconfig get" do
28+
assert FieldConfig.get(screens(:one), fields(:one), 'missing').nil?
29+
assert_equal 'valuehere', FieldConfig.get(screens(:one), fields(:one), 'keyname')
30+
end
2631
end

0 commit comments

Comments
 (0)