Module: Spree::Preferences::PreferableClassMethods

Defined in:
app/models/spree/preferences/preferable_class_methods.rb

Instance Method Summary collapse

Instance Method Details

#preference(name, type, *args) ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/spree/preferences/preferable_class_methods.rb', line 4

def preference(name, type, *args)
  options = args.extract_options!
  options.assert_valid_keys(:default, :description)
  default = options[:default]
  description = options[:description] || name

  # cache_key will be nil for new objects, then if we check if there
  # is a pending preference before going to default
  define_method preference_getter_method(name) do

    # perference_cache_key will only be nil/false for new records
    #
    if preference_cache_key(name)
      preference_store.get(preference_cache_key(name), default)
    else
      get_pending_preference(name) || default
    end
  end
  alias_method prefers_getter_method(name), preference_getter_method(name)

  define_method preference_setter_method(name) do |value|
    value = convert_preference_value(value, type)
    if preference_cache_key(name)
      preference_store.set preference_cache_key(name), value, type
    else
      add_pending_preference(name, value)
    end
  end
  alias_method prefers_setter_method(name), preference_setter_method(name)

  define_method preference_default_getter_method(name) do
    default
  end

  define_method preference_type_getter_method(name) do
    type
  end

  define_method preference_description_getter_method(name) do
    description
  end
end

#preference_default_getter_method(name) ⇒ Object


73
74
75
# File 'app/models/spree/preferences/preferable_class_methods.rb', line 73

def preference_default_getter_method(name)
  "preferred_#{name}_default".to_sym
end

#preference_description_getter_method(name) ⇒ Object


81
82
83
# File 'app/models/spree/preferences/preferable_class_methods.rb', line 81

def preference_description_getter_method(name)
  "preferred_#{name}_description".to_sym
end

#preference_getter_method(name) ⇒ Object


57
58
59
# File 'app/models/spree/preferences/preferable_class_methods.rb', line 57

def preference_getter_method(name)
  "preferred_#{name}".to_sym
end

#preference_setter_method(name) ⇒ Object


61
62
63
# File 'app/models/spree/preferences/preferable_class_methods.rb', line 61

def preference_setter_method(name)
   "preferred_#{name}=".to_sym
end

#preference_type_getter_method(name) ⇒ Object


77
78
79
# File 'app/models/spree/preferences/preferable_class_methods.rb', line 77

def preference_type_getter_method(name)
  "preferred_#{name}_type".to_sym
end

#prefers_getter_method(name) ⇒ Object


65
66
67
# File 'app/models/spree/preferences/preferable_class_methods.rb', line 65

def prefers_getter_method(name)
  "prefers_#{name}?".to_sym
end

#prefers_setter_method(name) ⇒ Object


69
70
71
# File 'app/models/spree/preferences/preferable_class_methods.rb', line 69

def prefers_setter_method(name)
   "prefers_#{name}=".to_sym
end

#remove_preference(name) ⇒ Object


47
48
49
50
51
52
53
54
55
# File 'app/models/spree/preferences/preferable_class_methods.rb', line 47

def remove_preference(name)
  remove_method preference_getter_method(name) if method_defined? preference_getter_method(name)
  remove_method preference_setter_method(name) if method_defined? preference_setter_method(name)
  remove_method prefers_getter_method(name) if method_defined? prefers_getter_method(name)
  remove_method prefers_setter_method(name) if method_defined? prefers_setter_method(name)
  remove_method preference_default_getter_method(name) if method_defined? preference_default_getter_method(name)
  remove_method preference_type_getter_method(name) if method_defined? preference_type_getter_method(name)
  remove_method preference_description_getter_method(name) if method_defined? preference_description_getter_method(name)
end