Skip to content

Commit 7a4f50f

Browse files
committed
Included ActionView::Helpers in Rabl::Engine
1 parent c2998e3 commit 7a4f50f

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

lib/gon/rabl.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'action_view'
12
require 'rabl'
23

34
module Gon
@@ -10,6 +11,8 @@ def handler(args, global = false)
1011
raise 'You should provide :template when use rabl with global variables'
1112
end
1213

14+
include_helpers
15+
1316
data = parse_rabl \
1417
Gon::Base.get_template_path(options, 'rabl'),
1518
Gon::Base.get_controller(options)
@@ -41,6 +44,12 @@ def parse_options_from(args)
4144
end
4245
end
4346

47+
def include_helpers
48+
unless ::Rabl::Engine.include? ::ActionView::Helpers
49+
::Rabl::Engine.send(:include, ::ActionView::Helpers)
50+
end
51+
end
52+
4453
def old_api?(args)
4554
args.first.is_a? String
4655
end

spec/gon/rabl_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
Gon.objects.length.should == 2
3333
end
3434

35+
it 'works with ActionView::Helpers' do
36+
Gon.rabl :template =>'spec/test_data/sample_with_helpers.rabl', :controller => controller
37+
Gon.objects.first['object']['time_ago'].should == 'about 6 hours'
38+
end
39+
3540
it 'raise exception if rabl is not included' do
3641
Gon.send :remove_const, 'Rabl'
3742
expect { Gon.rabl :template =>'spec/test_data/sample.rabl', :controller => controller}.to raise_error
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
collection @objects => 'objects'
2+
attributes :id
3+
node(:time_ago) { |_| distance_of_time_in_words(20000) }

0 commit comments

Comments
 (0)