Skip to content

Commit 0b4c057

Browse files
committed
0.2.1
1 parent dd0c4c2 commit 0b4c057

File tree

8 files changed

+52
-29
lines changed

8 files changed

+52
-29
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# CHANGELOG
22

3-
## 0.2.0
3+
## 0.2.1
44

5+
* refactor to avoid conflicts between classes
56
* `render_help_for` renamed to `render_hashtags_help_for`
67
* add `hashtags` class to the `dropdownClassName` option

lib/assets/javascripts/hashtags/hashtags.coffee

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
$.getJSON( url , q: term, class_name: data.class_name )
5757
.done( (resp) ->
5858
callback $.map( resp, (resource) ->
59+
console.log Handlebars.compile(match_template)(resource), term
5960
if Handlebars.compile(match_template)(resource).match(///#{term}///i) then resource else null
6061
)
6162
)

lib/hashtags/base.rb

+3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ def self.trigger
5151
raise NotImplementedError
5252
end
5353

54+
# matched against as user types
55+
# typically this would equal to the .template (match what you see)
5456
def self.match_template
57+
template
5558
end
5659

5760
# used to expire field with tags

lib/hashtags/mongoid_extension.rb

+23-20
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,41 @@
33

44
Mongoid::Fields.option :hashtags do |cls, field, value|
55
return if value == false
6+
return if cls.respond_to?(:hashtags)
67

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)) : {})
910

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
2018
end
2119

2220
field.define_singleton_method :demongoize do |*args|
2321
res = super(*args)
22+
2423
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)
2827
end
28+
2929
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)
3333
end
34+
3435
res
3536
end
3637

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)
3942
end
4043
end

lib/hashtags/resource.rb

-4
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ def self.match_index
5555
2
5656
end
5757

58-
def self.match_template
59-
"{{ #{tag_attribute} }}"
60-
end
61-
6258
def self.replace
6359
"#{trigger}#{resource_name}:{{ #{tag_attribute} }}({{ id }})"
6460
end

lib/hashtags/user.rb

-4
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ def self.match_index
5151
2
5252
end
5353

54-
def self.match_template
55-
"{{ #{tag_attribute} }}"
56-
end
57-
5854
def self.replace
5955
"#{trigger}{{ #{tag_attribute} }}"
6056
end

test/hashtags/mongoid_extension_test.rb

+18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
let(:str) { "User tag: #{user_tag}" }
99
let(:doc) { ExtensionDoc.new(text: str) }
10+
let(:doc2) { ExtensionDoc2.new(text: str) }
1011

1112
it { doc.text.must_equal str }
1213
it { doc.text.must_be_kind_of doc.class.fields['text'].type }
@@ -19,6 +20,12 @@
1920
doc.text.to_markup.must_equal "User tag: #{user.name}"
2021
end
2122
end
23+
24+
it 'does not convert if not supported' do
25+
::User.stub(:find, user_result) do
26+
doc2.text.to_markup.wont_equal "User tag: #{user.name}"
27+
end
28+
end
2229
end
2330

2431
describe '#to_hashtag' do
@@ -30,6 +37,12 @@
3037
doc.text.to_hashtag.must_equal 'User tag: @Sunny'
3138
end
3239
end
40+
41+
it 'does not convert if not supported' do
42+
::User.stub(:find, user_result) do
43+
doc2.text.to_hashtag.wont_equal 'User tag: @Sunny'
44+
end
45+
end
3346
end
3447

3548
describe '.hashtags' do
@@ -38,4 +51,9 @@
3851
it { ExtensionDoc.hashtags['text'].help.must_be_kind_of Array }
3952
it { ExtensionDoc.hashtags['text'].options.must_equal({}) }
4053
end
54+
55+
describe 'options defined on various classes should not influence each other' do
56+
it { ExtensionDoc.hashtags['text'].options.must_equal({}) }
57+
it { ExtensionDoc2.hashtags['text'].options.must_equal({ only: [VarTag] }) }
58+
end
4159
end

test/support/mongoid_extension_classes.rb

+5
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ class ExtensionDoc
22
include Mongoid::Document
33
field :text, type: String, hashtags: true
44
end
5+
6+
class ExtensionDoc2
7+
include Mongoid::Document
8+
field :text, type: String, hashtags: { only: [VarTag] }
9+
end

0 commit comments

Comments
 (0)