Created
August 6, 2014 20:13
-
-
Save phinze/0a323b902b44cee458a7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --cc lib/plugin_setting_with_account.rb | |
index 48c1009,3892c2e..0000000 | |
--- a/lib/plugin_setting_with_account.rb | |
+++ b/lib/plugin_setting_with_account.rb | |
@@@ -9,15 -9,47 +9,56 @@@ PluginSetting.class_eval d | |
def validate_uniqueness_of_name? | |
false | |
end | |
++<<<<<<< HEAD | |
+ | |
++======= | |
+ | |
+ def self.find_by_account_and_name(account, name) | |
+ setting = nil | |
+ remaining_shards = nil | |
+ case account | |
+ when Account | |
+ shard = account.shard | |
+ db_shard = shard.database_server.primary_shard | |
+ shard.activate do | |
+ if shard == db_shard || shard == Shard.default | |
+ remaining_shards = [Shard.default] unless shard == Shard.default | |
+ # look for both account settings and global settings | |
+ plugin_settings = PluginSetting. | |
+ where("account_id IS NULL OR account_id=?", account). | |
+ order(PluginSetting.nulls(:last, :account_id)) | |
+ else | |
+ remaining_shards = [db_shard, Shard.default].compact.uniq | |
+ # just look for account settings in this query | |
+ plugin_settings = account.plugin_settings | |
+ end | |
+ setting = plugin_settings.where(name: name.to_s).first | |
+ end | |
+ when :all | |
+ remaining_shards = [Shard.default] | |
+ when :db | |
+ remaining_shards = [Shard.current.database_server.primary_shard, Shard.default].compact.uniq | |
+ end | |
+ | |
+ if !setting && remaining_shards | |
+ Shard.with_each_shard(remaining_shards) do | |
+ setting = PluginSetting.where(name: name.to_s, account_id: nil).first | |
+ break if setting | |
+ end | |
+ end | |
+ | |
+ setting | |
+ end | |
+ | |
++>>>>>>> 8eb37e5... fix plugin setting inheritance cross-shard | |
def self.find_by_name(name) | |
account = PluginSetting.current_account | |
- find_by_account_and_name(account || :all, name) | |
+ if account.try(:shard).try(:default?) && Shard.current.default? | |
+ PluginSetting.where("name=? AND (account_id IS NULL OR account_id=?)", name.to_s, account).order("COALESCE(account_id, 0) DESC").first | |
+ else | |
+ result = account.plugin_settings.where(:name => name.to_s).first if account | |
+ result ||= Shard.default.activate { PluginSetting.where(:name => name.to_s, :account_id => nil).first } | |
+ end | |
end | |
def clear_cache_with_default | |
diff --cc spec_canvas/models/plugin_setting_spec.rb | |
index 0ea58ca,3a984dd..0000000 | |
--- a/spec_canvas/models/plugin_setting_spec.rb | |
+++ b/spec_canvas/models/plugin_setting_spec.rb | |
@@@ -106,6 -106,27 +106,30 @@@ describe PluginSetting, :type => :mode | |
PluginSetting.find_by_name(:qti_converter).should == settings | |
end | |
end | |
++<<<<<<< HEAD | |
++======= | |
+ | |
+ it "should inherit settings from the primary shard (primary shard)" do | |
+ @shard2.database_server.stubs(:primary_shard).returns(@shard2) | |
+ PluginSetting.create!(:name => 'qti_converter', :settings => { :enabled => 'global' }) | |
+ @shard2.activate do | |
+ PluginSetting.create!(:name => 'qti_converter', :settings => { :enabled => 'db' }) | |
+ @account = Account.create! | |
+ end | |
+ PluginSetting.current_account = @account | |
+ PluginSetting.find_by_name(:qti_converter).settings[:enabled].should == 'db' | |
+ end | |
+ | |
+ it "should inherit settings from the primary shard (different shard)" do | |
+ @shard1.database_server.stubs(:primary_shard).returns(Shard.default) | |
+ PluginSetting.create!(:name => 'qti_converter', :settings => { :enabled => 'global' }) | |
+ @shard1.activate do | |
+ @account = Account.create! | |
+ end | |
+ PluginSetting.current_account = @account | |
+ PluginSetting.find_by_name(:qti_converter).settings[:enabled].should == 'global' | |
+ end | |
++>>>>>>> 8eb37e5... fix plugin setting inheritance cross-shard | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment