mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-15 23:57:48 +00:00
Move Spree::Preferences::Configuration patches into superclass FileConfiguration
This commit is contained in:
@@ -1,12 +1,9 @@
|
||||
require 'open_food_network/paperclippable'
|
||||
|
||||
class ContentConfiguration < Spree::Preferences::Configuration
|
||||
class ContentConfiguration < Spree::Preferences::FileConfiguration
|
||||
# Header
|
||||
include OpenFoodNetwork::Paperclippable
|
||||
preference :logo_file_name, :string
|
||||
preference :logo_content_type, :string
|
||||
preference :logo_file_size, :integer
|
||||
preference :logo_updated_at, :string
|
||||
file_preference :logo
|
||||
has_attached_file :logo
|
||||
|
||||
# Home page
|
||||
|
||||
@@ -1,38 +1,4 @@
|
||||
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.to_sym)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
50
app/models/spree/preferences/file_configuration.rb
Normal file
50
app/models/spree/preferences/file_configuration.rb
Normal file
@@ -0,0 +1,50 @@
|
||||
module Spree::Preferences
|
||||
class FileConfiguration < Configuration
|
||||
|
||||
# Ideally, we'd alias_method_chain preference to add new type. However, failcake.
|
||||
def self.file_preference(name)
|
||||
preference "#{name}_file_name", :string
|
||||
preference "#{name}_content_type", :string
|
||||
preference "#{name}_file_size", :integer
|
||||
preference "#{name}_updated_at", :string
|
||||
end
|
||||
|
||||
|
||||
# TODO: Rewrite with super
|
||||
|
||||
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.to_sym)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -2,18 +2,15 @@ require 'spec_helper'
|
||||
|
||||
module Spree
|
||||
module Preferences
|
||||
class TestConfiguration < Configuration
|
||||
class TestConfiguration < FileConfiguration
|
||||
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
|
||||
file_preference :logo
|
||||
has_attached_file :logo
|
||||
end
|
||||
|
||||
describe Configuration do
|
||||
describe FileConfiguration do
|
||||
let(:c) { TestConfiguration.new }
|
||||
|
||||
describe "getting preferences" do
|
||||
Reference in New Issue
Block a user