Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Settings specs

  • Loading branch information...
commit 6f7de80a62aa722cb3dd4852c4ba336b19754bbe 1 parent 48f649b
@iain authored
View
4 app/models/settings.rb
@@ -3,10 +3,6 @@ class Settings
include ActiveModel::Conversion
include ActiveModel::Validations
- def self.fields
- Setting.all.map { |setting| "#{setting.group}_#{setting.key}" }
- end
-
def self.new(*)
attr_accessor *Setting.all.map(&:reader_name)
super
View
37 spec/models/settings_spec.rb
@@ -0,0 +1,37 @@
+require 'spec_helper'
+
+describe Settings do
+
+ it_should_behave_like ActiveModel
+
+ let!(:setting) { create :setting, :group => "foo", :key => "bar", :value => "quux" }
+
+ it "knows the value of the setting" do
+ subject.foo_bar.should == "quux"
+ end
+
+ it "will update values of settings" do
+ expect {
+ subject.save(setting.id => { :value => "baz" })
+ }.to change {
+ setting.reload.value
+ }.from("quux").to("baz")
+ end
+
+ it "will help build a form" do
+ names = []
+ ids = []
+ fields = []
+ subject.each do |group|
+ names << group.name
+ group.each do |id, field|
+ ids << id
+ fields << field
+ end
+ end
+ names.should == [ "foo" ]
+ ids.should == [ "foo_bar" ]
+ fields.should == [ "settings[#{setting.id}][value]" ]
+ end
+
+end
View
30 spec/support/active_model.rb
@@ -0,0 +1,30 @@
+require 'active_model'
+
+# spec/support/active_model_lint.rb
+# adapted from rspec-rails:
+# http://github.com/rspec/rspec-rails/blob/master/spec/rspec/rails/mocks/mock_model_spec.rb
+
+shared_examples_for ActiveModel do
+
+ include ActiveModel::Lint::Tests
+
+ # to_s is to support ruby-1.9
+ ActiveModel::Lint::Tests.public_instance_methods.map{|m| m.to_s}.grep(/^test/).each do |m|
+ example m.gsub('_',' ') do
+ send m
+ end
+ end
+
+ def model
+ subject
+ end
+
+ def assert(expected, message = nil)
+ expected.should be_true, message
+ end
+
+ def assert_kind_of(expected, actual, message = nil)
+ actual.should be_kind_of(expected), message
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.