mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-11 23:17:48 +00:00
Add file handling to Spree::Preferences::Configuration
This commit is contained in:
38
app/models/spree/preferences/configuration_decorator.rb
Normal file
38
app/models/spree/preferences/configuration_decorator.rb
Normal file
@@ -0,0 +1,38 @@
|
||||
module Spree::Preferences
|
||||
Configuration.class_eval do
|
||||
def get_preference_with_files(key)
|
||||
if !has_preference?(key) && has_attachment?(key)
|
||||
send(key)
|
||||
else
|
||||
get_preference_without_files(key)
|
||||
end
|
||||
end
|
||||
alias_method_chain :get_preference, :files
|
||||
alias :[] :get_preference
|
||||
|
||||
|
||||
def preference_type_with_files(name)
|
||||
if has_attachment? name
|
||||
:file
|
||||
else
|
||||
preference_type_without_files(name)
|
||||
end
|
||||
end
|
||||
alias_method_chain :preference_type, :files
|
||||
|
||||
|
||||
# Spree's Configuration responds to preference methods via method_missing, but doesn't
|
||||
# override respond_to?, which consequently reports those methods as unavailable. Paperclip
|
||||
# errors if respond_to? isn't correct, so we override it here.
|
||||
def respond_to?(method, include_all=false)
|
||||
name = method.to_s.gsub('=', '')
|
||||
super(self.class.preference_getter_method(name), include_all) || super(method, include_all)
|
||||
end
|
||||
|
||||
|
||||
def has_attachment?(name)
|
||||
self.class.respond_to?(:attachment_definitions) &&
|
||||
self.class.attachment_definitions.keys.include?(name)
|
||||
end
|
||||
end
|
||||
end
|
||||
60
spec/models/spree/preferences/configuration_spec.rb
Normal file
60
spec/models/spree/preferences/configuration_spec.rb
Normal file
@@ -0,0 +1,60 @@
|
||||
require 'spec_helper'
|
||||
|
||||
module Spree
|
||||
module Preferences
|
||||
class TestConfiguration < Configuration
|
||||
preference :name, :string
|
||||
|
||||
include OpenFoodNetwork::Paperclippable
|
||||
preference :logo_file_name, :string
|
||||
preference :logo_content_type, :string
|
||||
preference :logo_file_size, :integer
|
||||
preference :logo_updated_at, :string
|
||||
has_attached_file :logo
|
||||
end
|
||||
|
||||
describe Configuration do
|
||||
let(:c) { TestConfiguration.new }
|
||||
|
||||
describe "getting preferences" do
|
||||
it "returns regular preferences" do
|
||||
c.name = 'foo'
|
||||
c.get_preference(:name).should == 'foo'
|
||||
end
|
||||
|
||||
it "returns file preferences" do
|
||||
c.get_preference(:logo).should be_a Paperclip::Attachment
|
||||
end
|
||||
|
||||
it "returns regular preferences via []" do
|
||||
c.name = 'foo'
|
||||
c[:name].should == 'foo'
|
||||
end
|
||||
|
||||
it "returns file preferences via []" do
|
||||
c[:logo].should be_a Paperclip::Attachment
|
||||
end
|
||||
end
|
||||
|
||||
describe "getting preference types" do
|
||||
it "returns regular preference types" do
|
||||
c.preference_type(:name).should == :string
|
||||
end
|
||||
|
||||
it "returns file preference types" do
|
||||
c.preference_type(:logo).should == :file
|
||||
end
|
||||
end
|
||||
|
||||
describe "respond_to?" do
|
||||
it "responds to preference getters" do
|
||||
c.respond_to?(:name).should be_true
|
||||
end
|
||||
|
||||
it "responds to preference setters" do
|
||||
c.respond_to?(:name=).should be_true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user