|
3 | 3 |
|
4 | 4 | Mongoid::Fields.option :hashtags do |cls, field, value|
|
5 | 5 | return if value == false
|
| 6 | + return if cls.respond_to?(:hashtags) |
6 | 7 |
|
7 |
| - cls.define_singleton_method(:hashtags) { @hashtags ||= {} } unless cls.respond_to?(:hashtags) |
8 |
| - options = value.is_a?(Hash) ? value.slice(*%i(only except)) : {} |
| 8 | + cls.define_singleton_method :hashtags do |
| 9 | + options = (value.is_a?(Hash) ? value.slice(*%i(only except)) : {}) |
9 | 10 |
|
10 |
| - cls.hashtags[field.name].define_singleton_method :dom_data do |
11 |
| - Hashtags::Builder.dom_data(options) |
12 |
| - end |
13 |
| - |
14 |
| - cls.hashtags[field.name].define_singleton_method :help do |
15 |
| - Hashtags::Builder.help(options) |
16 |
| - end |
17 |
| - |
18 |
| - cls.hashtags[field.name].define_singleton_method :options do |
19 |
| - options |
| 11 | + @hashtags ||= {} |
| 12 | + @hashtags[field.name] ||= OpenStruct.new( |
| 13 | + dom_data: Hashtags::Builder.dom_data(options), |
| 14 | + help: Hashtags::Builder.help(options), |
| 15 | + options: options |
| 16 | + ) |
| 17 | + @hashtags |
20 | 18 | end
|
21 | 19 |
|
22 | 20 | field.define_singleton_method :demongoize do |*args|
|
23 | 21 | res = super(*args)
|
| 22 | + |
24 | 23 | res.define_singleton_method :to_markup do
|
25 |
| - field.type.new( |
26 |
| - Hashtags::Builder.to_markup(res.to_s, options).html_safe |
27 |
| - ) |
| 24 | + ho = field.options[:hashtags] |
| 25 | + options = (ho.is_a?(Hash) ? ho.slice(*%i(only except)) : {}) |
| 26 | + field.type.new(Hashtags::Builder.to_markup(res.to_s, options).html_safe) |
28 | 27 | end
|
| 28 | + |
29 | 29 | res.define_singleton_method :to_hashtag do
|
30 |
| - field.type.new( |
31 |
| - Hashtags::Builder.to_hashtag(res.to_s, options).html_safe |
32 |
| - ) |
| 30 | + ho = field.options[:hashtags] |
| 31 | + options = (ho.is_a?(Hash) ? ho.slice(*%i(only except)) : {}) |
| 32 | + field.type.new(Hashtags::Builder.to_hashtag(res.to_s, options).html_safe) |
33 | 33 | end
|
| 34 | + |
34 | 35 | res
|
35 | 36 | end
|
36 | 37 |
|
37 |
| - field.define_singleton_method :mongoize do |value| |
38 |
| - Hashtags::Builder.to_hashtag(super(value.to_s), options) |
| 38 | + field.define_singleton_method :mongoize do |val| |
| 39 | + ho = field.options[:hashtags] |
| 40 | + options = (ho.is_a?(Hash) ? ho.slice(*%i(only except)) : {}) |
| 41 | + Hashtags::Builder.to_hashtag(super(val.to_s), options) |
39 | 42 | end
|
40 | 43 | end
|
0 commit comments