Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don’t worry, you can still create the pull request.
  • 13 commits
  • 12 files changed
  • 0 commit comments
  • 1 contributor
View
87 app/models/config_manager2.rb
@@ -0,0 +1,87 @@
+require 'setting'
+module ConfigManager2
+ def self.included(base)
+ super
+ base.extend(ClassMethods)
+ end
+end
+
+ module ClassMethods
+ def acts_as_settable
+ add_finder()
+ add_settable_init()
+ add_loader()
+ end
+
+ def add_loader
+ self.send(:define_method, "load_settings") do
+ the_settings = Setting.find(:all, :conditions => ["setable_id = (?) and setable_type = (?)", id, self.class.name])
+ the_settings.each do |s|
+ name = s.setting_name + "="
+ self.send(name, s.setting_value)
+ end
+ end
+ end
+
+ def setting(name, type=:object, default=nil)
+ s = Setting.new(:setting_name => name, :setting_value => default, :setting_type => type.to_s, :setting_default => default)
+ add_setting_accessor(s)
+ end
+
+ def add_setting_accessor(setting)
+ add_setting_reader(setting)
+ add_setting_writer(setting)
+ end
+
+ def add_finder()
+ self.send(:define_method, "setting_for_name") do | name |
+ settings.each do |s|
+ if s.setting_name == name
+ return s
+ end
+ end
+ return nil
+ end
+ end
+
+ def add_settable_init()
+ self.send(:define_method, "settable_init") do | *args |
+ args.each do |key, value|
+ s = setting_for_name(key)
+ if not s.nil?
+ s.set_typed_value(val)
+ end
+ end
+ end
+ end
+
+ def add_setting_reader(setting)
+ key = setting.setting_name
+ self.send(:define_method, key) do
+ raw_value = setting_for_name(key)
+ raw_value.nil? ? setting.setting_default : raw_value.get_typed_value()
+ end
+ the_type = setting.setting_type.to_s
+ if (the_type == "boolean")
+ self.send(:define_method, "#{key}?") do
+ raw_value = setting_for_name(setting.setting_name)
+ raw_value.nil? ? setting.setting_default : raw_value.get_typed_value()
+ end
+ end
+ end
+
+
+ def add_setting_writer(setting)
+ self.send(:define_method, "#{setting.setting_name}=") do |newvalue|
+ key = setting.setting_name
+ the_setting = setting_for_name(key)
+ if the_setting.nil?
+ the_setting = settings.build(:setting_name => key)
+ end
+ the_setting.set_typed_value(newvalue)
+ the_setting.setting_default = setting.setting_default
+ newvalue
+ end
+ end
+
+end
View
50 app/models/setting.rb
@@ -0,0 +1,50 @@
+class Setting < ActiveRecord::Base
+ belongs_to :setable, :polymorphic => true
+
+ attr_accessible :setting_name, :setting_value, :setting_default, :setting_type
+
+ def set_typed_value(value)
+ write_attribute(:setting_value, value.to_s)
+ write_attribute(:setting_type, type_from_value(value))
+ end
+
+ def get_typed_value
+ value = read_attribute(:setting_value)
+ typename = read_attribute(:setting_type)
+ case typename
+ when "boolean"
+ case value
+ when "0", 0, '', false, "false", "f", nil
+ false
+ else
+ true
+ end
+ when "integer"
+ value.to_i
+ when "string"
+ value.to_s
+ when "yaml"
+ value.to_yaml
+ else
+ value
+ end
+ end
+
+ private
+
+ def type_from_value(value)
+ theClass = value.class
+ if theClass == TrueClass or theClass == FalseClass
+ "boolean"
+ elsif theClass == Fixnum
+ "integer"
+ elsif theClass == String
+ "string"
+ else
+ nil
+ end
+ end
+
+
+
+end
View
24 app/models/user.rb
@@ -1,8 +1,9 @@
require 'digest/sha1'
+require 'config_manager2'
# Typo user.
class User < ActiveRecord::Base
- include ConfigManager
+ include ConfigManager2
belongs_to :profile
belongs_to :text_filter
@@ -17,9 +18,15 @@ class User < ActiveRecord::Base
has_many :articles, :order => 'created_at DESC'
- serialize :settings, Hash
+ has_many :settings, :as => :setable, :autosave => true
+
+ Setting.includes(:setable)
+
+# serialize :settings, Hash
# Settings
+ acts_as_settable
+
setting :notify_watch_my_articles, :boolean, true
setting :editor, :string, 'visual'
setting :firstname, :string, ''
@@ -47,14 +54,23 @@ def self.salt
'20ac4d290c2293702c64b3b287ae5ea79b26a5c1'
end
+ after_save :save_settings
+
+ def save_settings
+ self.settings.each do | s |
+ s.save!
+ end
+ end
+
+ after_find :load_settings
+
attr_accessor :last_venue
def initialize(*args)
super
- self.settings ||= {}
+ settable_init(*args)
end
-
def self.authenticate(login, pass)
find(:first,
:conditions => ["login = ? AND password = ? AND state = ?", login, password_hash(pass), 'active'])
View
15 db/migrate/106_create_settings_table.rb
@@ -0,0 +1,15 @@
+class CreateSettingsTable < ActiveRecord::Migration
+ def self.up
+ create_table :settings do |t|
+ t.references :setable, :polymorphic => :true
+ t.column :setting_name, :string
+ t.column :setting_type, :string
+ t.column :setting_default, :string
+ t.column :setting_value, :text
+ end
+ end
+
+ def self.down
+ drop_table :settings
+ end
+end
View
1 spec/controllers/admin/content_controller_spec.rb
@@ -619,6 +619,7 @@ def base_article(options={})
before :each do
FactoryGirl.create(:blog)
+ Profile.delete(:all)
@user = FactoryGirl.create(:user,
text_filter: FactoryGirl.create(:markdown),
profile: FactoryGirl.create(:profile_publisher))
View
22 spec/controllers/admin/users_controller_spec.rb
@@ -125,4 +125,26 @@
end
end
end
+
+ describe 'At all times when working with settings' do
+ it 'should save and reload defaults for editor from session' do
+ u = FactoryGirl.create(:user, :editor => "visual")
+ u.save!
+ session[:user] = u.id
+ u2 = User.find_by_id(u.id)
+ u2.editor.should == "visual"
+ u2.simple_editor?.should be_false
+ User.delete_all
+ end
+
+ it 'should save and reload modified default for editor from session' do
+ u = FactoryGirl.create(:user, :editor => "simple")
+ u.save!
+ session[:user] = u.id
+ u2 = User.find_by_id(u.id)
+ u2.editor.should == "simple"
+ u2.simple_editor?.should be_true
+ User.delete_all
+ end
+ end
end
View
17 spec/controllers/articles_controller_spec.rb
@@ -11,7 +11,8 @@
:password => 'whatever',
:name => 'Henri',
:email => 'henri@example.com',
- :settings => {:notify_watch_my_articles => false, :editor => 'simple'},
+ :notify_watch_my_articles => false,
+ :editor => 'simple',
:text_filter => FactoryGirl.create(:markdown),
:profile => FactoryGirl.create(:profile_admin, :label => Profile::ADMIN),
:notify_via_email => false,
@@ -360,7 +361,8 @@
:password => 'whatever',
:name => 'Henri',
:email => 'henri@example.com',
- :settings => {:notify_watch_my_articles => false, :editor => 'simple'},
+ :notify_watch_my_articles => false,
+ :editor => 'simple',
:text_filter => FactoryGirl.create(:markdown),
:profile => FactoryGirl.create(:profile_admin, :label => Profile::ADMIN),
:notify_via_email => false,
@@ -381,7 +383,8 @@
:password => 'whatever',
:name => 'Henri',
:email => 'henri@example.com',
- :settings => {:notify_watch_my_articles => false, :editor => 'simple'},
+ :notify_watch_my_articles => false,
+ :editor => 'simple',
:text_filter => FactoryGirl.create(:markdown),
:profile => FactoryGirl.create(:profile_admin, :label => Profile::ADMIN),
:notify_via_email => false,
@@ -408,7 +411,8 @@
:password => 'whatever',
:name => 'Henri',
:email => 'henri@example.com',
- :settings => {:notify_watch_my_articles => false, :editor => 'simple'},
+ :notify_watch_my_articles => false,
+ :editor => 'simple',
:text_filter => FactoryGirl.create(:markdown),
:profile => FactoryGirl.create(:profile_admin, :label => Profile::ADMIN),
:notify_via_email => false,
@@ -688,7 +692,8 @@
:password => 'whatever',
:name => 'Henri',
:email => 'henri@example.com',
- :settings => {:notify_watch_my_articles => false, :editor => 'simple'},
+ :notify_watch_my_articles => false,
+ :editor => 'simple',
:text_filter => FactoryGirl.create(:markdown),
:profile => FactoryGirl.create(:profile_admin, :label => Profile::ADMIN),
:notify_via_email => false,
@@ -765,4 +770,4 @@
assigns[:page].should == @page
end
end
-end
+end
View
3 spec/controllers/backend_controller_spec.rb
@@ -13,7 +13,8 @@
:password => 'whatever',
:name => 'Henri',
:email => 'henri@example.com',
- :settings => {:notify_watch_my_articles => false, :editor => 'simple'},
+ :notify_watch_my_articles => false,
+ :editor => 'simple',
:text_filter => FactoryGirl.create(:markdown),
:profile => FactoryGirl.create(:profile_admin, :label => Profile::ADMIN),
:notify_via_email => false,
View
26 spec/factories.rb
@@ -48,13 +48,37 @@ def some_article
u.notify_via_email false
u.notify_on_new_articles false
u.notify_on_comments false
+ u.editor ''
+ u.notify_watch_my_articles false
+ u.admin_theme ''
u.password 'top-secret'
- u.settings({})
u.state 'active'
u.profile {FactoryGirl.create(:profile)}
u.text_filter {FactoryGirl.create(:textile)}
end
+ factory :user_with_settings, class: User, parent: :user do |u|
+ u.notify_watch_my_articles true
+ u.editor 'simple'
+ u.firstname 'Melvin'
+ u.lastname 'WatchPocket'
+ u.nickname 'Melvin the Bomb'
+ u.description "A rambler and a gambler and a sweet talkin' lady's man"
+ u.url 'http://www.particlewave.com'
+ u.msn 'StillUsingHotmail@Hotmail.com'
+ u.aim 'to please'
+ u.yahoo 'MelvinW@yahoo.com'
+ u.twitter '@Choo'
+ u.jabber 'mw'
+ u.show_url true
+ u.show_msn false
+ u.show_aim false
+ u.show_yahoo true
+ u.show_twitter true
+ u.show_jabber false
+ u.admin_theme 'red'
+ end
+
factory :article do |a|
a.title 'A big article'
a.body 'A content with several data'
View
72 spec/models/user_spec.rb
@@ -232,4 +232,76 @@ def set_password(newpass)
user.simple_editor?.should be_false
end
end
+
+ def hash_fill(hash)
+ hash.keys.each do |k|
+ hash[k] = "true"
+ end
+ end
+
+ describe "User Settings" do
+ it "should save and reload all options" do
+ user = FactoryGirl.build(:user_with_settings)
+ user.save!
+ user2 = User.find_by_email(user.email)
+ user2.editor.should == user.editor
+ user.settings.each do |s|
+ user2.settings.each do |s2|
+ if s.setting_name == s2.setting_name then
+ s.setting_value.should == s2.setting_value
+ end
+ end
+ end
+ end
+
+ it "should not interfere with text_filter" do
+ user = FactoryGirl.create(:user, :profile => FactoryGirl.create(:profile_admin, :label => Profile::ADMIN))
+ user.text_filter.name.should == "textile"
+ end
+
+ it "should have visual editor by default" do
+ user = User.new
+ user.editor.should == "visual"
+ end
+
+ it "should report non-simple editor" do
+ user = User.new
+ user.simple_editor?.should be_false
+ end
+
+ it "should allow editor override in FactoryGirl" do
+ user = FactoryGirl.create(:user_with_settings, :editor => 'bogus')
+ user.simple_editor?.should be_false
+ user.editor.should == 'bogus'
+ end
+
+ it "should allow editor override in simple case" do
+ user = User.new(:editor => "simple")
+ user.simple_editor?.should be_true
+ user.editor.should == 'simple'
+ end
+
+ it "should save editor override" do
+ user = FactoryGirl.create(:user_with_settings, :editor => 'bogus')
+ user.save!
+ user.simple_editor?.should be_false
+ user.editor.should == 'bogus'
+ u2 = User.find_by_id(user.id)
+ u2.simple_editor?.should be_false
+ u2.editor.should == 'bogus'
+ User.delete_all
+ end
+
+ it "should save editor default" do
+ user = FactoryGirl.create(:user, :editor => 'simple')
+ user.save!
+ user.simple_editor?.should be_true
+ user.editor.should == 'simple'
+ u2 = User.find_by_id(user.id)
+ u2.simple_editor?.should be_true
+ u2.editor.should == 'simple'
+ User.delete_all
+ end
+ end
+
end
View
2 spec/views/admin/content/new_spec.rb
@@ -2,7 +2,7 @@
describe "admin/content/new.html.erb" do
before do
- admin = stub_model(User, :settings => {:editor => 'simple'}, :admin? => true,
+ admin = stub_model(User, :editor => 'simple', :admin? => true,
:text_filter_name => "", :profile_label => "admin")
blog = mock_model(Blog, :base_url => "http://myblog.net/")
article = stub_model(Article).as_new_record
View
2 spec/views/admin/pages/new_spec.rb
@@ -2,7 +2,7 @@
describe "admin/pages/new.html.erb" do
before do
- admin = stub_model(User, :settings => { :editor => 'simple' }, :admin? => true,
+ admin = stub_model(User, :editor => 'simple', :admin? => true,
:text_filter_name => "", :profile_label => "admin")
blog = mock_model(Blog, :base_url => "http://myblog.net/")
page = stub_model(Page).as_new_record

No commit comments for this range

Something went wrong with that request. Please try again.