Skip to content

Commit fe0bd1b

Browse files
committed
controller etc
1 parent 6e00517 commit fe0bd1b

15 files changed

+74
-83
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
module Hashtags
22
class ResourcesController < ApplicationController
3-
respond_to :json
4-
53
def index
6-
respond_with resources_as_json, root: false
4+
respond_to do |format|
5+
format.json { render json: resources_as_json, root: false }
6+
end
77
end
88

99
private
1010

1111
def resources_as_json
12-
return unless hashtag_class
13-
# @resources_as_json ||= hashtag_resource_class.as_json(query)
14-
end
15-
16-
def hashtag_class
17-
Resource.find_by_resource_type(resource_type)
12+
return unless hashtag_class.present?
13+
@resources_as_json ||= hashtag_class.resources_for_query(query)
1814
end
1915

2016
def query
2117
URI.decode(params.fetch(:q, nil).to_s)
2218
end
2319

24-
def resource_type
25-
params[:resource_type]
20+
def hashtag_class
21+
class_name.safe_constantize
22+
end
23+
24+
def class_name
25+
params[:class_name]
2626
end
2727
end
2828
end

config/routes.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
Hashtags::Engine.routes.draw do
2-
get 'resources' => 'resources#index', as: :resources
2+
namespace :hashtags do
3+
get 'resources' => 'resources#index', as: :resources
4+
end
35
end

lib/assets/javascripts/hashtags/hashtags.coffee

+2-4
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@
3333
context: (text) -> text.toLowerCase()
3434
match: ///#{data.match_regexp}///
3535
search: (term, callback, match) =>
36-
if data.values
37-
@search_values(data, term, callback, match)
38-
else
39-
@search_remote(data, term, callback, match)
36+
if data.values then @search_values(data, term, callback, match)
37+
else @search_remote(data, term, callback, match)
4038
replace: (resource, event) -> Handlebars.compile(data.replace)(resource)
4139
template: (resource, term) -> Handlebars.compile(data.template)(resource)
4240
}

lib/hashtags/base.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def self.template
105105

106106
# return JSON version of resources that match query
107107
# this is returned when user starts typing (the query)
108-
def self.resources_for_query(query)
108+
def self.json_for_query(query)
109109
raise NotImplementedError
110110
end
111111

lib/hashtags/builder.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def to_hashtag(str)
3333
# render textcomplete dom data
3434
def dom_data
3535
{ hashtags: {
36-
path: Engine.routes.url_helpers.resources_path,
36+
path: Engine.routes.url_helpers.hashtags_resources_path,
3737
strategies: hashtag_strategies
3838
} }
3939
end

lib/hashtags/resource.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def self.help_values
4848
# JS
4949

5050
def self.match_regexp
51-
/(#{trigger}#{resource_name}\:)(\w*)\z/
51+
/(#{Regexp.escape(trigger)}#{resource_name}\:)(\w{1,})\z/
5252
end
5353

5454
def self.match_index

lib/hashtags/user.rb

+7-7
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,19 @@ def self.help_values
4444
# JS
4545

4646
def self.match_regexp
47-
/(\A#{trigger}|\s#{trigger})(\w{1,})\z/
47+
/(\A#{Regexp.escape(trigger)}|\s#{Regexp.escape(trigger)})(\w{1,})\z/
4848
end
4949

5050
def self.match_index
51-
1
51+
2
5252
end
5353

5454
def self.match_template
5555
"{{ #{tag_attribute} }}"
5656
end
5757

5858
def self.replace
59-
"$1{{ #{tag_attribute} }}"
59+
"#{trigger}{{ #{tag_attribute} }}"
6060
end
6161

6262
def self.template
@@ -68,24 +68,24 @@ def self.template
6868
# updates found tags with tag value from resource
6969
# @jtschichold => @JTschichold
7070
def hashtag(match)
71-
return unless id = match[self.class.match_index]
71+
return unless id = match[self.class.match_index-1]
7272
return unless user = resource(id)
7373
Handlebars::Context.new
74-
.compile(self.class.replace.gsub('$1', Regexp.escape(self.class.trigger)))
74+
.compile(self.class.replace)
7575
.call(self.class.tag_attribute => user.send(self.class.tag_attribute))
7676
end
7777

7878
# replaces tags with result from resource
7979
# @JTschichold => Jan Tschichold
8080
def markup(match)
81-
return unless id = match[self.class.match_index]
81+
return unless id = match[self.class.match_index-1]
8282
return unless user = resource(id)
8383
user.send(self.class.result_attribute)
8484
end
8585

8686
# finds resource based on tag_attribute_value
8787
# for example: resource_class.where(username: tag_attribute_value).first
88-
def resource(tag_attribute_value)
88+
def resource(value)
8989
raise NotImplemented
9090
end
9191
end

lib/hashtags/variable.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ def self.help_values
2020
# JS
2121

2222
def self.match_regexp
23-
/(\A#{Regexp.escape(trigger)}|\s#{Regexp.escape(trigger)})(\w*)\z/
23+
/(\A#{Regexp.escape(trigger)}|\s#{Regexp.escape(trigger)})(\w{1,})\z/
2424
end
2525

2626
def self.match_index
2727
2
2828
end
2929

3030
def self.replace
31-
"#{trigger}1{{ this }}"
31+
"#{trigger}{{ this }}"
3232
end
3333

3434
def self.template

test/hashtags/builder_test.rb

+5-19
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
require 'test_helper'
22

33
describe Hashtags::Builder do
4-
let(:user) { ::User.new('JTschichold', 'Jan Tschichold') }
4+
let(:user) { ::User.find('JTschichold') }
55
let(:user_tag) { "@#{user.id}" }
66

7-
let(:res) { ::MyResource.new('123', 'Resource') }
7+
let(:res) { ::MyResource.find(1) }
88
let(:resource_name) { 'my_resource' }
99
let(:res_tag) { "##{resource_name}:#{res.title}(#{res.id})" }
1010

@@ -19,13 +19,7 @@
1919
let(:options) { {} }
2020

2121
describe '.to_markup' do
22-
let(:to_markup) do
23-
::User.stub(:find, user_result) do
24-
::MyResource.stub(:find, resource_result) do
25-
Hashtags::Builder.to_markup(str, options)
26-
end
27-
end
28-
end
22+
let(:to_markup) { Hashtags::Builder.to_markup(str, options) }
2923

3024
it { to_markup.must_equal "User tag: #{user.name}, resource tag: #{res.title}, variable tag: #{var_1}" }
3125

@@ -41,17 +35,9 @@
4135
end
4236

4337
describe '.to_hashtag' do
44-
let(:to_hashtag) do
45-
::User.stub(:find, user_result) do
46-
::MyResource.stub(:find, resource_result) do
47-
Hashtags::Builder.to_hashtag(str, options)
48-
end
49-
end
50-
end
38+
let(:to_hashtag) { Hashtags::Builder.to_hashtag(str, options) }
5139

52-
before do
53-
user.id = 'Sunny'
54-
end
40+
before { user.id = 'Sunny' }
5541

5642
it { to_hashtag.must_equal "User tag: @Sunny, resource tag: #{res_tag}, variable tag: #{var_tag}" }
5743

test/hashtags/resource_test.rb

+6-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
require 'test_helper'
22

33
describe Hashtags::Resource do
4-
let(:res_1) { ::MyResource.new('123', 'Resource 1') }
5-
let(:res_2) { ::MyResource.new('456', 'Resource 2') }
4+
let(:res_1) { ::MyResource.find(1) }
5+
let(:res_2) { ::MyResource.find(2) }
66
let(:str) { "Resources: #my_resource:#{res_1.title}(#{res_1.id}) & #my_resource:#{res_2.title}(#{res_2.id})" }
77

88
subject { MyResourceTag.new(str) }
@@ -11,24 +11,14 @@
1111
it { Hashtags::Resource.find_by_resource_type(:my_resource).must_equal MyResourceTag }
1212

1313
describe '#to_markup' do
14-
it 'should replace hastags with markup' do
15-
find_result = lambda do |id|
16-
case id
17-
when res_1.id then res_1
18-
when res_2.id then res_2
19-
end
20-
end
21-
22-
::MyResource.stub(:find, find_result) do
23-
subject.to_markup.must_equal "Resources: #{res_1.title} & #{res_2.title}"
24-
end
25-
end
14+
it { subject.to_markup.must_equal "Resources: #{res_1.title} & #{res_2.title}" }
2615
end
2716

2817
describe '#to_hashtag' do
29-
it 'updates the original str with new values' do
18+
before do
19+
res_1.id = 3
3020
res_1.title = 'Sunny'
31-
subject.to_hashtag.must_equal "Resources: #my_resource:Sunny(#{res_1.id}) & #my_resource:#{res_2.title}(#{res_2.id})"
3221
end
22+
it { subject.to_hashtag.must_equal "Resources: #my_resource:Sunny(#{res_1.id}) & #my_resource:#{res_2.title}(#{res_2.id})" }
3323
end
3424
end

test/hashtags/user_test.rb

+5-18
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,20 @@
11
require 'test_helper'
22

33
describe Hashtags::User do
4-
let(:user_1) { ::User.new('JTschichold', 'Jan Tschichold') }
5-
let(:user_2) { ::User.new('KGerstner', 'Karl Gerstner') }
4+
let(:user_1) { ::User.find('JTschichold') }
5+
let(:user_2) { ::User.find('KGerstner') }
66
let(:str) { "Say hello to @#{user_1.id} and @#{user_2.id}!" }
77

88
subject { UserTag.new(str) }
99

1010
it { UserTag.cache_key.must_equal User.cache_key }
1111

1212
describe '#to_markup' do
13-
it 'should replace hastags with markup' do
14-
find_result = lambda do |id|
15-
case id
16-
when user_1.id then user_1
17-
when user_2.id then user_2
18-
end
19-
end
20-
21-
::User.stub(:find, find_result) do
22-
subject.to_markup.must_equal "Say hello to #{user_1.name} and #{user_2.name}!"
23-
end
24-
end
13+
it { subject.to_markup.must_equal "Say hello to #{user_1.name} and #{user_2.name}!" }
2514
end
2615

2716
describe '#to_hashtag' do
28-
it 'updates the original str with new values' do
29-
user_1.id = 'Sunny'
30-
subject.to_hashtag.must_equal "Say hello to @Sunny and @#{user_2.id}!"
31-
end
17+
before { user_1.id = 'Sunny' }
18+
it { subject.to_hashtag.must_equal "Say hello to @Sunny and @#{user_2.id}!" }
3219
end
3320
end

test/support/resource_classes.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ def self.cache_key
33
'cache_key'
44
end
55

6-
def self.find(value)
6+
def self.all
7+
[
8+
new(1, 'Resource 1'),
9+
new(2, 'Resource 2')
10+
]
11+
end
12+
13+
def self.find(id)
14+
all.detect{ |i| i.id == id.to_i }
715
end
816
end

test/support/resource_tags.rb

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ def self.result_attribute
1111
:title
1212
end
1313

14+
def self.resources_for_query(query)
15+
resource_class.all.select do |res|
16+
res.title =~ /#{Regexp.escape(query)}/i
17+
end
18+
end
19+
1420
def resource(value)
1521
self.class.resource_class.find(value)
1622
end

test/support/user_classes.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@ def self.cache_key
33
'cache_key'
44
end
55

6-
def self.find(value)
6+
def self.all
7+
[
8+
new('JTschichold', 'Jan Tschichold'),
9+
new('KGerstner', 'Karl Gerstner')
10+
]
11+
end
12+
13+
def self.find(id)
14+
all.detect{ |i| i.id == id.to_s }
715
end
816

917
def to_s

test/support/user_tags.rb

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ def self.result_attribute
1111
:name
1212
end
1313

14+
def self.resources_for_query(query)
15+
resource_class.all.select do |res|
16+
res.id =~ /#{Regexp.escape(query)}/i
17+
end
18+
end
19+
1420
def resource(value)
1521
self.class.resource_class.find(value)
1622
end

0 commit comments

Comments
 (0)