2013-06-22 22:35:06 -05:00
|
|
|
module SiteSettings; end
|
|
|
|
|
|
|
|
class SiteSettings::DbProvider
|
|
|
|
|
|
|
|
def initialize(model)
|
2014-03-30 20:34:01 -05:00
|
|
|
model.after_commit do
|
|
|
|
model.notify_changed!
|
|
|
|
end
|
|
|
|
|
2013-06-22 22:35:06 -05:00
|
|
|
@model = model
|
|
|
|
end
|
|
|
|
|
|
|
|
def all
|
|
|
|
return [] unless table_exists?
|
|
|
|
|
2015-09-09 08:34:53 -05:00
|
|
|
# Not leaking out AR records, cause I want all editing to happen via this API
|
|
|
|
SqlBuilder.new("SELECT name, data_type, value FROM #{@model.table_name}").map_exec(OpenStruct)
|
2013-06-22 22:35:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def find(name)
|
|
|
|
return nil unless table_exists?
|
|
|
|
|
2015-09-09 08:34:53 -05:00
|
|
|
# Not leaking out AR records, cause I want all editing to happen via this API
|
2015-09-09 05:59:49 -05:00
|
|
|
SqlBuilder.new("SELECT name, data_type, value FROM #{@model.table_name} WHERE name = :name")
|
2013-06-22 22:35:06 -05:00
|
|
|
.map_exec(OpenStruct, name: name)
|
|
|
|
.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def save(name, value, data_type)
|
|
|
|
return unless table_exists?
|
|
|
|
|
2015-09-09 05:59:49 -05:00
|
|
|
model = @model.find_by(name: name)
|
2014-03-30 20:34:01 -05:00
|
|
|
model ||= @model.new
|
|
|
|
|
|
|
|
model.name = name
|
2017-07-27 20:20:09 -05:00
|
|
|
model.value = value
|
|
|
|
model.data_type = data_type
|
2014-03-30 20:34:01 -05:00
|
|
|
|
|
|
|
# save! used to ensure after_commit is called
|
|
|
|
model.save!
|
2013-06-22 22:35:06 -05:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy(name)
|
|
|
|
return unless table_exists?
|
|
|
|
|
|
|
|
@model.where(name: name).destroy_all
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_site
|
|
|
|
RailsMultisite::ConnectionManagement.current_db
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
# table is not in the db yet, initial migration, etc
|
|
|
|
def table_exists?
|
2017-08-07 02:37:42 -05:00
|
|
|
@table_exists ||= {}
|
|
|
|
|
|
|
|
unless @table_exists[current_site]
|
|
|
|
@table_exists[current_site] = ActiveRecord::Base.connection.table_exists?(@model.table_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
@table_exists[current_site]
|
2013-06-22 22:35:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|