Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add the base Dynamic Content class.

  • Loading branch information...
commit c909dcc87eb9e4627cc34c13290ffca151138895 1 parent 847f423
@bamnet bamnet authored
View
31 app/models/dynamic_content.rb
@@ -0,0 +1,31 @@
+class DynamicContent < Content
+ after_initialize :set_kind, :create_config
+
+ after_find :load_config
+ before_validation :save_config
+
+ attr_accessor :config
+
+ # Automatically set the kind for the content
+ # if it is new. We use this hidden type that no fields
+ # render so Dynamic Content meta content never gets displayed.
+ def set_kind
+ return unless new_record?
+ self.kind = Kind.where(:name => 'Dynamic').first
+ end
+
+ # Create a new configuration hash if necessary.
+ def create_config
+ self.config = self.config || {}
+ end
+
+ # Load a configuration hash back from the database to attribute.
+ def load_config
+ self.config = JSON.load(self.data)
+ end
+
+ # Prepare the configuration to be saved.
+ def save_config
+ self.data = JSON.dump(self.config)
+ end
+end
View
2  db/seeds.rb
@@ -8,7 +8,7 @@
# but I don't have a solid grasp on the system-wide reprecussions of that
# change at the moment.
-["Graphics", "Ticker", "Text"].each do |kind|
+["Graphics", "Ticker", "Text", "Dynamic"].each do |kind|
Kind.find_or_create_by_name kind
end
View
37 test/unit/dynamic_content_test.rb
@@ -0,0 +1,37 @@
+require 'test_helper'
+include ActionDispatch::TestProcess
+
+class DynamicContentTest < ActiveSupport::TestCase
+ test "New config is created" do
+ dynamic = DynamicContent.new
+ assert_equal dynamic.config.class, {}.class
+ dynamic.config['var'] = 'foo'
+ assert_equal dynamic.config['var'], 'foo'
+ end
+
+ test "Save and load config" do
+ dynamic = DynamicContent.new
+ dynamic.config['var'] = 'foo'
+ dynamic.config['other'] = 123
+ dynamic.save_config
+
+ dynamic.config = nil
+ dynamic.load_config
+
+ assert_equal dynamic.config['var'], 'foo'
+ assert_equal dynamic.config['other'], 123
+ end
+
+ test "Auto save and load" do
+ dynamic = DynamicContent.new
+ dynamic.name = 'Dynamic Content'
+ dynamic.user = users(:katie)
+ dynamic.config['var'] = 'foo'
+ dynamic.config['other'] = 123
+ dynamic.save
+
+ fresh = DynamicContent.find(dynamic.id)
+ assert_equal fresh.config['var'], 'foo'
+ assert_equal fresh.config['other'], 123
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.