mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 09:36:19 -05:00
PluginStore for plugin specific bits of storage
Amended plugin interfaces so they work with the vk sample
This commit is contained in:
parent
213ce33af2
commit
818bf1355d
9 changed files with 117 additions and 8 deletions
|
@ -9,6 +9,8 @@
|
||||||
//= require_tree ./application
|
//= require_tree ./application
|
||||||
//= require ./foundation/helpers
|
//= require ./foundation/helpers
|
||||||
<%
|
<%
|
||||||
|
# TODO this is very tricky, we want to add a dependency here on files that may not yet exist
|
||||||
|
# otherwise in dev we are often stuck nuking the tmp/cache directory
|
||||||
DiscoursePluginRegistry.stylesheets.each do |css|
|
DiscoursePluginRegistry.stylesheets.each do |css|
|
||||||
require_asset(css)
|
require_asset(css)
|
||||||
end
|
end
|
||||||
|
|
40
app/models/plugin_store.rb
Normal file
40
app/models/plugin_store.rb
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
# API to wrap up plugin store rows
|
||||||
|
class PluginStore
|
||||||
|
def self.get(plugin_name, key)
|
||||||
|
if row = PluginStoreRow.where(plugin_name: plugin_name, key: key).first
|
||||||
|
cast_value(row.type_name, row.value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.set(plugin_name, key, value)
|
||||||
|
hash = {plugin_name: plugin_name, key: key}
|
||||||
|
row = PluginStoreRow.where(hash).first || row = PluginStoreRow.new(hash)
|
||||||
|
|
||||||
|
row.type_name = determine_type(value)
|
||||||
|
# nil are stored as nil
|
||||||
|
row.value =
|
||||||
|
if row.type_name == "JSON"
|
||||||
|
value.to_json
|
||||||
|
elsif value
|
||||||
|
value.to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
row.save
|
||||||
|
end
|
||||||
|
|
||||||
|
protected
|
||||||
|
|
||||||
|
|
||||||
|
def self.determine_type(value)
|
||||||
|
value.is_a?(Hash) ? "JSON" : value.class.to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.cast_value(type, value)
|
||||||
|
case type
|
||||||
|
when "Fixnum" then value.to_i
|
||||||
|
when "TrueClass", "FalseClass" then value == "true"
|
||||||
|
when "JSON" then ActiveSupport::HashWithIndifferentAccess.new(::JSON.parse(value))
|
||||||
|
else value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
2
app/models/plugin_store_row.rb
Normal file
2
app/models/plugin_store_row.rb
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
class PluginStoreRow < ActiveRecord::Base
|
||||||
|
end
|
|
@ -119,9 +119,9 @@ module Discourse
|
||||||
# attr_accessible.
|
# attr_accessible.
|
||||||
config.active_record.whitelist_attributes = false
|
config.active_record.whitelist_attributes = false
|
||||||
|
|
||||||
|
require 'plugin'
|
||||||
|
require 'auth'
|
||||||
unless Rails.env.test?
|
unless Rails.env.test?
|
||||||
require 'plugin'
|
|
||||||
require_dependency 'auth'
|
|
||||||
Discourse.activate_plugins!
|
Discourse.activate_plugins!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
15
db/migrate/20130826011521_create_plugin_store_rows.rb
Normal file
15
db/migrate/20130826011521_create_plugin_store_rows.rb
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
class CreatePluginStoreRows < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
create_table :plugin_store_rows do |table|
|
||||||
|
table.string :plugin_name, null: false
|
||||||
|
table.string :key, null: false
|
||||||
|
table.string :type_name, null: false
|
||||||
|
# not the most efficient implementation but will do for now
|
||||||
|
# possibly in future we can add more tables so int and boolean etc values are
|
||||||
|
# not stored in text
|
||||||
|
table.text :value
|
||||||
|
end
|
||||||
|
|
||||||
|
add_index :plugin_store_rows, [:plugin_name, :key], unique: true
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,5 +1,9 @@
|
||||||
class Plugin::AuthProvider
|
class Plugin::AuthProvider
|
||||||
attr_accessor :type, :glyph, :background_color, :name, :title,
|
attr_accessor :glyph, :background_color, :title,
|
||||||
:message, :frame_width, :frame_height, :authenticator
|
:message, :frame_width, :frame_height, :authenticator
|
||||||
|
|
||||||
|
def name
|
||||||
|
authenticator.name
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -153,14 +153,12 @@ class Plugin::Instance
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def auth_provider(type, opts)
|
def auth_provider(opts)
|
||||||
@auth_providers ||= []
|
@auth_providers ||= []
|
||||||
provider = Plugin::AuthProvider.new
|
provider = Plugin::AuthProvider.new
|
||||||
provider.type = type
|
[:glyph, :background_color, :title, :message, :frame_width, :frame_height, :authenticator].each do |sym|
|
||||||
[:name, :glyph, :background_color, :title, :message, :frame_width, :frame_height, :authenticator].each do |sym|
|
|
||||||
provider.send "#{sym}=", opts.delete(sym)
|
provider.send "#{sym}=", opts.delete(sym)
|
||||||
end
|
end
|
||||||
provider.name ||= type.to_s
|
|
||||||
@auth_providers << provider
|
@auth_providers << provider
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,6 @@ describe Plugin::Instance do
|
||||||
|
|
||||||
plugin.auth_providers.count.should == 1
|
plugin.auth_providers.count.should == 1
|
||||||
auth_provider = plugin.auth_providers[0]
|
auth_provider = plugin.auth_providers[0]
|
||||||
auth_provider.options.should == {:identifier => 'https://zappa.com'}
|
|
||||||
auth_provider.type.should == :open_id
|
auth_provider.type.should == :open_id
|
||||||
|
|
||||||
# calls ensure_assets! make sure they are there
|
# calls ensure_assets! make sure they are there
|
||||||
|
|
49
spec/models/plugin_store_spec.rb
Normal file
49
spec/models/plugin_store_spec.rb
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
require "spec_helper"
|
||||||
|
require_dependency "plugin_store"
|
||||||
|
|
||||||
|
describe PluginStore do
|
||||||
|
def set(k,v)
|
||||||
|
PluginStore.set("my_plugin", k, v)
|
||||||
|
end
|
||||||
|
|
||||||
|
def get(k)
|
||||||
|
PluginStore.get("my_plugin", k)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "sets strings correctly" do
|
||||||
|
set("hello", "world")
|
||||||
|
expect(get("hello")).to eq("world")
|
||||||
|
|
||||||
|
set("hello", "world1")
|
||||||
|
expect(get("hello")).to eq("world1")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "sets fixnums correctly" do
|
||||||
|
set("hello", 1)
|
||||||
|
expect(get("hello")).to eq(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "sets bools correctly" do
|
||||||
|
set("hello", true)
|
||||||
|
expect(get("hello")).to eq(true)
|
||||||
|
|
||||||
|
set("hello", false)
|
||||||
|
expect(get("hello")).to eq(false)
|
||||||
|
|
||||||
|
set("hello", nil)
|
||||||
|
expect(get("hello")).to eq(nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
it "handles hashes correctly" do
|
||||||
|
|
||||||
|
val = {"hi" => "there", "1" => 1}
|
||||||
|
set("hello", val)
|
||||||
|
result = get("hello")
|
||||||
|
|
||||||
|
expect(result).to eq(val)
|
||||||
|
|
||||||
|
# ensure indiff access holds
|
||||||
|
expect(result[:hi]).to eq("there")
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue