From e4a4cdd9150e99aa107742c661b4d5cf618241b6 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 17 Jul 2015 15:22:44 +1000 Subject: [PATCH] Rewrite alias_method_chain to use super. Ahhhhh :) --- app/models/content_configuration.rb | 2 +- .../spree/preferences/file_configuration.rb | 36 +++++++++---------- .../preferences/file_configuration_spec.rb | 2 +- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/app/models/content_configuration.rb b/app/models/content_configuration.rb index 32a2065c5d..0bf4c4e852 100644 --- a/app/models/content_configuration.rb +++ b/app/models/content_configuration.rb @@ -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 diff --git a/app/models/spree/preferences/file_configuration.rb b/app/models/spree/preferences/file_configuration.rb index 5d84e37fdf..fa1838a778 100644 --- a/app/models/spree/preferences/file_configuration.rb +++ b/app/models/spree/preferences/file_configuration.rb @@ -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 diff --git a/spec/models/spree/preferences/file_configuration_spec.rb b/spec/models/spree/preferences/file_configuration_spec.rb index 77aabc637e..eb18b23e12 100644 --- a/spec/models/spree/preferences/file_configuration_spec.rb +++ b/spec/models/spree/preferences/file_configuration_spec.rb @@ -6,7 +6,7 @@ module Spree preference :name, :string include OpenFoodNetwork::Paperclippable - file_preference :logo + preference :logo, :file has_attached_file :logo end