link2 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'link2'
@@ -0,0 +1,111 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class BrainTest < ActionView::TestCase
5
+
6
+ include ::Link2::Brain
7
+
8
+ def setup
9
+ ::I18n.locale = :en
10
+
11
+ @mookey = ::Fraggle.create(:name => 'Mookey')
12
+ end
13
+
14
+ test "#resource_identifier_class?: should only be true for valid classes" do
15
+ assert resource_identifier_class?(nil)
16
+ assert resource_identifier_class?(:hello)
17
+ assert resource_identifier_class?(::Fraggle)
18
+
19
+ assert_not resource_identifier_class?(::Unicorn)
20
+ end
21
+
22
+ test "#record_class?: should only be true for record classes" do
23
+ assert record_class?(::Fraggle)
24
+
25
+ assert_not record_class?(::Unicorn)
26
+ end
27
+
28
+ test "#controller_name_for_resource: should return controller name based on resource" do
29
+ assert_equal 'fraggles', controller_name_for_resource(::Fraggle)
30
+ assert_equal 'fraggles', controller_name_for_resource(::Fraggle.new)
31
+ assert_equal 'fraggles', controller_name_for_resource(:fraggle)
32
+ end
33
+
34
+ test "#controller_name_for_resource: should return current controller if no resource is specified or is nil" do
35
+ # Current: TestController
36
+ assert_equal 'test', controller_name_for_resource(nil)
37
+ end
38
+
39
+ test "#localized_label: should pass current controller to I18n options" do
40
+ swap ::Link2, :i18n_scopes => ['{{controller}}.links.{{action}}'] do
41
+ store_translations :en, {:fraggles => {:links => {:hello => "Wassup?!"}}} do
42
+ assert_equal "Wassup?!", localized_label(:hello, ::Fraggle)
43
+ end
44
+ end
45
+ end
46
+
47
+ test "#label_and_url_for_resource: should parse a label and url from a object (e.g. @post)" do
48
+ store_translations :en, {:links => {:show => "Show {{resource}}"}} do
49
+ assert_equal ["Show fraggle", fraggle_path(@mookey)], label_and_url_for_resource(@mookey)
50
+ end
51
+ end
52
+
53
+ test "#label_and_url_for_resource: should parse a label and url from a object (e.g. @post) with respect to to_s-value" do
54
+ store_translations :en, {:links => {:show => "Show {{name}}"}} do
55
+ @mookey.class_eval do
56
+ def to_s
57
+ ""
58
+ end
59
+ end
60
+ assert_equal ["Show fraggle", fraggle_path(@mookey)], label_and_url_for_resource(@mookey)
61
+
62
+ @mookey.class_eval do
63
+ def to_s
64
+ self.name
65
+ end
66
+ end
67
+ assert_equal ["Show Mookey", fraggle_path(@mookey)], label_and_url_for_resource(@mookey)
68
+ end
69
+ end
70
+
71
+ test "#label_and_url_for_resource: should parse correct label and url from a polymorphic args (e.g. [@user, :blog, @post])" do
72
+ @mookeys_cool_aid = ::CoolAid.create(:name => 'Super-tasty')
73
+
74
+ store_translations :en, {:links => {:show => "Show {{name}}"}} do
75
+ @mookeys_cool_aid.class_eval do
76
+ def to_s
77
+ self.name
78
+ end
79
+ end
80
+
81
+ assert_equal ["Show Super-tasty", fraggle_cool_aid_path(@mookey, @mookeys_cool_aid)], label_and_url_for_resource([@mookey, @mookeys_cool_aid])
82
+ end
83
+ end
84
+
85
+ test "#url_for_args: should use explicitly passed url if any" do
86
+ assert_equal '/posts', url_for_args(:new, '/posts')
87
+ assert_equal '/', url_for_args(:back, '/')
88
+ end
89
+
90
+ test "#url_for_args: should generate a url based on action only if mapping for this action exists" do
91
+ assert_equal '/', url_for_args(:home)
92
+ assert_equal :back, url_for_args(:back) # fix this to handle session[:return_to] in proc.
93
+ end
94
+
95
+ test "#url_for_args: should generate a url based on action and resource" do
96
+ # FIXME: stub(:session).returns({:return_to => '/'})
97
+
98
+ self.expects(:controller_name_for_resource).with(@mookey).returns('fraggles').twice
99
+ self.expects(:controller_name_for_resource).with(::Fraggle).returns('fraggles')
100
+ self.expects(:controller_name_for_resource).with(:fraggle).returns('fraggles')
101
+
102
+ assert_equal "/fraggles/new?id=#{@mookey.id}", url_for_args(:new, @mookey)
103
+ assert_equal '/fraggles/new', url_for_args(:new, ::Fraggle)
104
+ assert_equal '/fraggles/new', url_for_args(:new, :fraggle)
105
+
106
+ assert_equal "/fraggles/#{@mookey.id}/edit", url_for_args(:edit, @mookey)
107
+ end
108
+
109
+ # NOTICE: #link_to_args tested fully in HelpersTest as helpers are sort of identical.
110
+
111
+ end
@@ -0,0 +1,125 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class HelpersTest < ActionView::TestCase
5
+
6
+ include Link2::Helpers
7
+
8
+ def setup
9
+ ::I18n.locale = :en
10
+
11
+ @mookey = ::Fraggle.create(:name => 'Mookey')
12
+ @mookeys_cool_aid = ::CoolAid.create(:name => 'Super-tasty')
13
+ end
14
+
15
+ # link({}, {})
16
+
17
+ test "link() should raise error" do
18
+ assert_raise(::ArgumentError) { link }
19
+ end
20
+
21
+ # link(x, {}, {})
22
+
23
+ test "link(url) should render link_to(url, url)" do
24
+ assert_equal link_to('http://example.com', 'http://example.com'), link('http://example.com')
25
+ assert_equal link_to('/posts?by=date', '/posts?by=date'), link('/posts?by=date')
26
+ end
27
+
28
+ test "link(label) should render link_to(str_label, '#')" do
29
+ assert_equal link_to('Hello', '#'), link('Hello')
30
+ end
31
+
32
+ test "link(:action) should render link_to(t(:action, ...), url_for(:action => :action, ...)), auto-detecting resource" do
33
+ swap ::Link2, :i18n_scopes => ['link.{{action}}'] do
34
+ # assert_equal link_to("New Fraggle"), link(:new)
35
+ assert_raise(::Link2::NotImplementedYetError) { link(:new) }
36
+ end
37
+ end
38
+
39
+ test "link(:mapping) should render link_to(t(:mapping, ...), url_for_mapping(:mapping, ...)), auto-detecting resource" do
40
+ swap ::Link2, :i18n_scopes => ['link.{{action}}'] do
41
+ assert_equal link_to("Home", '/'), link(:home)
42
+
43
+ swap ::Link2, :action_mappings => {:secret => '/secret'} do
44
+ assert_equal link_to("Secret", '/secret'), link(:secret)
45
+ end
46
+ end
47
+ end
48
+
49
+ test "link(@resource) should render link_to(t(:show, ...), @object)" do
50
+ swap ::Link2, :i18n_scopes => ['link.{{action}}'] do
51
+ assert_equal link_to("Show", "/fraggles/#{@mookey.id}"), link(@mookey)
52
+ # assert_equal link_to("Show", "?"), link(::Fraggle) # test this stupid case?
53
+ end
54
+ end
55
+
56
+ test "link([@parent, @resource]) should render link_to(t(:show, ...), polymorphic_path([@parent, @resource]))" do
57
+ swap ::Link2, :i18n_scopes => ['link.{{action}}'] do
58
+ assert_equal link_to("Show", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}"), link([@mookey, @mookeys_cool_aid])
59
+ end
60
+ end
61
+
62
+ # FINALIZE: link(x, y, {}, {})
63
+
64
+ test "link(label, url) should render link_to(label, url)" do
65
+ assert_equal link_to('http://example.com', 'http://example.com'), link('http://example.com', 'http://example.com')
66
+ assert_equal link_to('New', '/posts/new'), link('New', '/posts/new')
67
+ end
68
+
69
+ test "link(:action) should render link_to(label, url_for(:action => :action, ...)), auto-detecting resource" do
70
+ swap ::Link2, :i18n_scopes => ['link.{{action}}'] do
71
+ # assert_equal link_to("New Fraggle!!"), link("New Fraggle!!", :new)
72
+ assert_raise(::Link2::NotImplementedYetError) { link("New Fraggle!!", :new) }
73
+ end
74
+ end
75
+
76
+ test "link(label, action) should render link_to(label, url_for_mapping(:mapping, ...)), auto-detecting resource" do
77
+ swap ::Link2, :i18n_scopes => ['link.{{action}}'] do
78
+ assert_equal link_to("Home!!", '/'), link("Home!!", :home)
79
+
80
+ swap ::Link2, :action_mappings => {:secret => '/secret'} do
81
+ assert_equal link_to("Damn you!!", '/secret'), link("Damn you!!", :secret)
82
+ end
83
+ end
84
+ end
85
+
86
+ test "link(:action, Resource) should render link_to(t(:action, ...), url_for(:action => :action, ...))" do
87
+ swap ::Link2, :i18n_scopes => ['link.{{action}}'] do
88
+ assert_equal link_to("New", "/fraggles/new"), link(:new, ::Fraggle)
89
+ end
90
+ end
91
+
92
+ test "link(:action, @resource) should render link_to(t(:action, ...), url_for(:action => :action, ...))" do
93
+ swap ::Link2, :i18n_scopes => ['link.{{action}}'] do
94
+ # Non-RESTful-route, basic case.
95
+ assert_equal link_to("New", "/fraggles/new?id=#{@mookey.id}"), link(:new, @mookey)
96
+ # REST-case.
97
+ assert_equal link_to("Edit", "/fraggles/#{@mookey.id}/edit"), link(:edit, @mookey)
98
+ end
99
+ end
100
+
101
+ test "link(:action, [@parent, @resource]) should render link_to(t(:action, ...), polymorphic_path([@parent, @resource]), :action => :action)" do
102
+ swap ::Link2, :i18n_scopes => ['link.{{action}}'] do
103
+ # assert_equal link_to("Edit", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}/edit"), link(:edit, [@mookey, @mookeys_cool_aid])
104
+ assert_raise(::Link2::NotImplementedYetError) { link(:edit, [@mookey, @mookeys_cool_aid]) }
105
+ end
106
+ end
107
+
108
+ # link(x, y, z, {}, {})
109
+
110
+ test "link(label, action, resource)" do
111
+ assert_equal link_to("Newish", "/fraggles/new"), link("Newish", :new, ::Fraggle)
112
+ assert_equal link_to("Editish", "/fraggles/#{@mookey.id}/edit"), link("Editish", :edit, @mookey)
113
+ end
114
+
115
+ test "js_link should not be implemented (yet)" do
116
+ assert_raise(::Link2::NotImplementedYetError) { js_link(:alert, 'alert("New");', {}, {}) }
117
+ assert_raise(::Link2::NotImplementedYetError) { js_button(:alert, 'alert("New");', {}, {}) }
118
+ end
119
+
120
+ test "ajax_link should not be implemented (yet)" do
121
+ assert_raise(::Link2::NotImplementedYetError) { ajax_link(:home, {}, {}) }
122
+ assert_raise(::Link2::NotImplementedYetError) { ajax_button(:home, {}, {}) }
123
+ end
124
+
125
+ end
data/test/i18n_test.rb ADDED
@@ -0,0 +1,65 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class I18nTest < ActiveSupport::TestCase
5
+
6
+ def setup
7
+ ::I18n.locale = :en
8
+ end
9
+
10
+ test "i18n: should load default translations automatically" do
11
+ options = {:scope => 'links', :resource => 'post', :resources => 'posts'}
12
+
13
+ ::I18n.backend.reload!
14
+
15
+ assert_equal "Home", ::I18n.t(:home, options)
16
+ assert_equal "Back", ::I18n.t(:back, options)
17
+
18
+ assert_equal "New post", ::I18n.t(:new, options)
19
+ assert_equal "Edit post", ::I18n.t(:edit, options)
20
+ assert_equal "Delete post", ::I18n.t(:delete, options)
21
+ assert_equal "Show post", ::I18n.t(:show, options)
22
+ assert_equal "Index of posts", ::I18n.t(:index, options)
23
+ end
24
+
25
+ test "i18n: should substitute scopes with parsed values for: controller, action, resource, resources" do
26
+ dummie_scopes = ['{{controller}}.{{resources}}.{{resource}}.{{action}}.label', 'links.{{action}}']
27
+ expected_substitution = [:'fraggles.fraggles.fraggle.new.label', :'links.new']
28
+
29
+ swap ::Link2, :i18n_scopes => dummie_scopes do
30
+ assert_raise(::Link2::I18n::ScopeInterpolationError) { ::Link2::I18n.send(:substituted_scopes_for, :new, ::Fraggle) }
31
+
32
+ assert_nothing_raised(::KeyError) { ::Link2::I18n.send(:substituted_scopes_for, :new, ::Fraggle, :controller => 'fraggles') }
33
+ assert_equal expected_substitution, ::Link2::I18n.send(:substituted_scopes_for, :new, ::Fraggle, :controller => 'fraggles')
34
+ end
35
+ end
36
+
37
+ test "i18n: should be able to translate action without any options" do
38
+ i18n_options = {:scope => 'links', :resource => 'fraggle', :resources => 'fraggles'}
39
+
40
+ assert_equal ::I18n.t(:new, i18n_options), ::Link2::I18n.t(:new, ::Fraggle)
41
+ assert_equal ::I18n.t(:new, i18n_options), ::Link2::I18n.t(:new, ::Fraggle.new)
42
+ assert_equal ::I18n.t(:new, i18n_options), ::Link2::I18n.t(:new, :fraggle)
43
+ end
44
+
45
+ test "i18n: should be able to translate action with respect to any valid extra interpolation arguments" do
46
+ swap ::Link2, :i18n_scopes => ['links.{{action}}'] do
47
+ store_translations :en, {:links => {:shout => 'Hello {{nick}}!'}} do
48
+ i18n_options = {:scope => 'links', :nick => 'Mokey'}
49
+
50
+ assert_equal ::I18n.t(:shout, i18n_options), ::Link2::I18n.t(:shout, ::Fraggle.new, :nick => 'Mokey')
51
+ end
52
+ end
53
+ end
54
+
55
+ test "i18n: should not interpolate values for any reserved interpolation keys" do
56
+ swap ::Link2, :i18n_scopes => ['links.{{action}}'] do
57
+ store_translations :en, {:links => {:shout => 'Hello {{scope}}!'}} do
58
+ i18n_options = {:scope => 'links', :name => 'Mokey'}
59
+
60
+ assert_raise(I18n::ReservedInterpolationKey) { ::Link2::I18n.t(:shout, ::Fraggle.new, :scope => 'Mokey') }
61
+ end
62
+ end
63
+ end
64
+
65
+ end
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+
3
+ class Link2Test < ActiveSupport::TestCase
4
+
5
+ test "#setup: setup block yields self" do
6
+ Link2.setup do |config|
7
+ assert_equal Link2, config
8
+ end
9
+ end
10
+
11
+ test "#setup: should be configurable using setup helper" do
12
+ swap Link2, :i18n_scopes => ['links.{{action}}'] do
13
+ assert_equal ['links.{{action}}'], Link2.i18n_scopes
14
+ end
15
+
16
+ swap Link2, :action_mappings => {:home => '/'} do
17
+ assert_equal ({:home => '/'}), Link2.action_mappings
18
+ end
19
+ end
20
+
21
+ test "#url_for_mapping: should only map mapped action keys" do
22
+ swap Link2, :action_mappings => {:home_sweet_home => '/welcome'} do
23
+ assert_equal '/welcome', Link2.url_for_mapping(:home_sweet_home)
24
+ assert_nil Link2.url_for_mapping(:unicorns_and_rainbows)
25
+ end
26
+ end
27
+
28
+ test "#url_for_mapping: should be able to map procs" do
29
+ swap Link2, :action_mappings => {:home_sweet_home => lambda { '/welcome' }} do
30
+ assert_equal '/welcome', Link2.url_for_mapping(:home_sweet_home)
31
+ end
32
+ end
33
+
34
+ test "#url_for_mapping: should be able to map procs with custom url" do
35
+ swap Link2, :action_mappings => {:home_sweet_home => lambda { |url| url || '/welcome' }} do
36
+ #assert_equal '/get_lost', Link2.url_for_mapping(:home_sweet_home, '/get_lost')
37
+ end
38
+ end
39
+
40
+ # FAILS - see TODO:
41
+ # test "#url_for_mapping: should be able to map procs containing named route (or helper method)" do
42
+ # swap Link2, :action_mappings => {:root => lambda { root_path }} do
43
+ # assert_equal root_path, Link2.url_for_mapping(:root)
44
+ # end
45
+ # end
46
+
47
+ end
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+
3
+ module Link2
4
+ module AssertionsHelper
5
+ def assert_not(assertion)
6
+ assert !assertion
7
+ end
8
+
9
+ def assert_blank(assertion)
10
+ assert assertion.blank?
11
+ end
12
+
13
+ def assert_not_blank(assertion)
14
+ assert !assertion.blank?
15
+ end
16
+ alias :assert_present :assert_not_blank
17
+
18
+ def assert_no_select(*args)
19
+ assert_raise Test::Unit::AssertionFailedError do
20
+ assert_select(*args)
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ ActiveSupport::TestCase.class_eval do
27
+ include Link2::AssertionsHelper
28
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+
3
+ ActiveRecord::Migration.verbose = false
4
+ ActiveRecord::Base.logger = Logger.new(nil)
5
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
6
+
7
+ ActiveSupport::TestCase.class_eval do
8
+ # self.use_transactional_fixtures = true
9
+ # self.use_instantiated_fixtures = false
10
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ require 'i18n'
3
+
4
+ module Link2
5
+ module DebugHelper
6
+
7
+ def debug_routes
8
+ ::ActionController::Routing::Routes.named_routes.each do |name, route|
9
+ puts "%20s: %s" % [name, route]
10
+ end
11
+ end
12
+
13
+ end
14
+ end
15
+
16
+ ActiveSupport::TestCase.class_eval do
17
+ include Link2::DebugHelper
18
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+ require 'i18n'
3
+
4
+ module Link2
5
+ module SubstitutionsHelper
6
+
7
+ # Execute the block setting the given values and restoring old values after
8
+ # the block is executed.
9
+ def swap(object, new_values)
10
+ old_values = {}
11
+ new_values.each do |key, value|
12
+ old_values[key] = object.send key
13
+ object.send :"#{key}=", value
14
+ end
15
+ yield
16
+ ensure
17
+ old_values.each do |key, value|
18
+ object.send :"#{key}=", value
19
+ end
20
+ end
21
+
22
+ def store_translations(locale, translations, &block)
23
+ current_translations = ::I18n.backend.send(:translations).send(:[], locale.to_sym)
24
+
25
+ begin
26
+ ::I18n.backend.store_translations locale, translations
27
+ yield
28
+ ensure
29
+ # ::I18n.reload!
30
+ end
31
+ end
32
+
33
+ end
34
+ end
35
+
36
+ ActiveSupport::TestCase.class_eval do
37
+ include Link2::SubstitutionsHelper
38
+ end