Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 390
- Log:
Initial import of Canvass, a donations-based poll application.
- Author:
- rool
- Date:
- Mon Mar 21 14:58:04 +0000 2011
- Size:
- 4826 Bytes
1 | require File.dirname(__FILE__) + '/spec_helper' |
2 | require File.dirname(__FILE__) + '/cases/topic' |
3 | |
4 | describe TranslatableColumns do |
5 | |
6 | before :each do |
7 | @languages = %w{en nl de fr} |
8 | @columns = @languages.map{|l|"title_#{l}"} |
9 | @topic = Topic.new |
10 | Topic.stub!(:find).and_return(@topic) |
11 | |
12 | # return these persistent values to their defaults |
13 | ActiveRecord::Base.translatable_columns_config.set_defaults |
14 | I18n.locale = 'nl-NL' |
15 | I18n.default_locale = 'en-US' |
16 | end |
17 | |
18 | it "should have a mocked model" do |
19 | Topic.column_names.should include("title_en") |
20 | end |
21 | |
22 | it "should find all localized columns" do |
23 | Topic.available_translatable_columns_of(:title).should include(*@columns) |
24 | end |
25 | |
26 | it "should get the language of a locale" do |
27 | Topic.column_locale('nl-BE').should == 'nl' |
28 | end |
29 | |
30 | it "should change nl-BE to nl_be because of database usage" do |
31 | ActiveRecord::Base.translatable_columns_config.full_locale = true |
32 | Topic.column_locale('nl-BE').should == 'nl_be' |
33 | end |
34 | |
35 | it "should return a localized column" do |
36 | Topic.column_name_localized('title', 'nl-NL').should == 'title_nl' |
37 | end |
38 | |
39 | it "should check for existence of localized columns" do |
40 | Topic.translated_column_exists?('title', 'nl-NL').should be_true |
41 | Topic.translated_column_exists?('title', 'jp-JP').should be_false |
42 | end |
43 | |
44 | it "should find the default column when asking for a non existing locale" do |
45 | Topic.column_translated('title', 'jp-JP').should == 'title_en' |
46 | end |
47 | |
48 | it "should find the column when asking for an existing locale" do |
49 | Topic.column_translated('title', 'nl-NL').should == 'title_nl' |
50 | end |
51 | |
52 | it "should define an accessor" do |
53 | @topic.should respond_to(:title=) |
54 | @topic.should respond_to(:title) |
55 | @topic.should respond_to(:body=) |
56 | @topic.should respond_to(:body) |
57 | end |
58 | |
59 | it "should define a reader using defaults" do |
60 | Topic.should_receive(:define_translated_getter_with_defaults) |
61 | Topic.translatable_columns(:title) |
62 | end |
63 | |
64 | it "should define a reader without using defaults" do |
65 | Topic.should_receive(:define_translated_getter_without_defaults) |
66 | Topic.translatable_columns(:title, :use_default => false) |
67 | end |
68 | |
69 | it "should define a reader without using defaults, because of config" do |
70 | ActiveRecord::Base.translatable_columns_config.use_default = false |
71 | Topic.should_receive(:define_translated_getter_without_defaults) |
72 | Topic.translatable_columns(:title) |
73 | end |
74 | |
75 | it "should define a reader with defaults, inspite of config" do |
76 | ActiveRecord::Base.translatable_columns_config.use_default = false |
77 | Topic.should_receive(:define_translated_getter_with_defaults) |
78 | Topic.translatable_columns(:title, :use_default => true) |
79 | end |
80 | |
81 | it "should find a value in any column" do |
82 | @columns.each do |column| |
83 | @topic.send(:"#{column}=", column) |
84 | @topic.find_translated_value_for(:title).should == column |
85 | @topic = Topic.new # reset |
86 | end |
87 | end |
88 | |
89 | it "should retrieve the translated value, not using defaults" do |
90 | Topic.translatable_columns :title, :use_default => false |
91 | @topic.should_receive(:title_nl).and_return("foo") |
92 | @topic.title.should == "foo" |
93 | end |
94 | |
95 | it "should retrieve the translated value, using defaults" do |
96 | Topic.translatable_columns :title, :use_default => true |
97 | @topic.should_receive(:title_nl).and_return("foo") |
98 | @topic.title.should == "foo" |
99 | end |
100 | |
101 | it "should retrieve the default translation value when the locale doesn't exist, using no defaults" do |
102 | I18n.locale = 'jp-JP' |
103 | Topic.translatable_columns :title, :use_default => false |
104 | @topic.should_receive(:title_en).and_return("foo") |
105 | @topic.title.should == 'foo' |
106 | end |
107 | |
108 | it "should retrieve the default translation value when the locale doesn't exist, when using defaults" do |
109 | I18n.locale = 'jp-JP' |
110 | Topic.translatable_columns :title, :use_default => true |
111 | @topic.should_receive(:title_en).and_return("foo") |
112 | @topic.title.should == "foo" |
113 | end |
114 | |
115 | it "should find any value when using defaults" do |
116 | @topic.should_receive(:title_fr).at_least(:once).and_return("foo") |
117 | @topic.title.should == "foo" |
118 | end |
119 | |
120 | it "should set a value for the current locale" do |
121 | @topic.should_receive(:title_nl=) |
122 | @topic.title = "foo" |
123 | end |
124 | |
125 | it "should return a value for the default locale" do |
126 | I18n.locale = 'jp-JP' |
127 | @topic.should_receive(:title_en=) |
128 | @topic.title = "foo" |
129 | end |
130 | |
131 | it "should validate at least one translation" do |
132 | @topic.valid?.should be_false |
133 | @topic.should have(1).error_on(:title) |
134 | @topic.should have(1).error_on(:body) |
135 | end |
136 | |
137 | it "should have a :must_have_translation error message" do |
138 | options = [:blank, { :default => :must_have_translation }] |
139 | @topic.errors.should_receive(:add).with(:title, *options) |
140 | @topic.errors.should_receive(:add).with(:body, *options) |
141 | @topic.valid? |
142 | end |
143 | |
144 | end |