Rewrite alias_method_chain to use super. Ahhhhh :)

This commit is contained in:
Rohan Mitchell
2015-07-17 15:22:44 +10:00
parent 66b4eb4c5d
commit e4a4cdd915
3 changed files with 20 additions and 20 deletions

View File

@@ -3,7 +3,7 @@ require 'open_food_network/paperclippable'
class ContentConfiguration < Spree::Preferences::FileConfiguration
# Header
include OpenFoodNetwork::Paperclippable
file_preference :logo
preference :logo, :file
has_attached_file :logo
# Home page

View File

@@ -1,36 +1,36 @@
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
def self.preference(name, type, *args)
if type == :file
super "#{name}_file_name", :string, *args
super "#{name}_content_type", :string, *args
super "#{name}_file_size", :integer, *args
super "#{name}_updated_at", :string, *args
# 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)
super name, type, *args
end
end
def get_preference(key)
if !has_preference?(key) && has_attachment?(key)
send key
else
super key
end
end
alias_method_chain :get_preference, :files
alias :[] :get_preference
def preference_type_with_files(name)
def preference_type(name)
if has_attachment? name
:file
else
preference_type_without_files(name)
super name
end
end
alias_method_chain :preference_type, :files
# Spree's Configuration responds to preference methods via method_missing, but doesn't

View File

@@ -6,7 +6,7 @@ module Spree
preference :name, :string
include OpenFoodNetwork::Paperclippable
file_preference :logo
preference :logo, :file
has_attached_file :logo
end