yeshua_crm 1.0.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.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/Rakefile +11 -0
  3. data/lib/yeshoua_crm.rb +87 -0
  4. data/lib/yeshua_crm/acts_as_draftable/draft.rb +40 -0
  5. data/lib/yeshua_crm/acts_as_draftable/rcrm_acts_as_draftable.rb +154 -0
  6. data/lib/yeshua_crm/acts_as_list/list.rb +282 -0
  7. data/lib/yeshua_crm/acts_as_taggable/rcrm_acts_as_taggable.rb +350 -0
  8. data/lib/yeshua_crm/acts_as_taggable/tag.rb +81 -0
  9. data/lib/yeshua_crm/acts_as_taggable/tag_list.rb +111 -0
  10. data/lib/yeshua_crm/acts_as_taggable/tagging.rb +16 -0
  11. data/lib/yeshua_crm/acts_as_viewed/rcrm_acts_as_viewed.rb +274 -0
  12. data/lib/yeshua_crm/acts_as_votable/rcrm_acts_as_votable.rb +80 -0
  13. data/lib/yeshua_crm/acts_as_votable/rcrm_acts_as_voter.rb +20 -0
  14. data/lib/yeshua_crm/acts_as_votable/votable.rb +323 -0
  15. data/lib/yeshua_crm/acts_as_votable/vote.rb +28 -0
  16. data/lib/yeshua_crm/acts_as_votable/voter.rb +131 -0
  17. data/lib/yeshua_crm/assets_manager.rb +43 -0
  18. data/lib/yeshua_crm/currency/formatting.rb +224 -0
  19. data/lib/yeshua_crm/currency/heuristics.rb +151 -0
  20. data/lib/yeshua_crm/currency/loader.rb +24 -0
  21. data/lib/yeshua_crm/currency.rb +439 -0
  22. data/lib/yeshua_crm/helpers/external_assets_helper.rb +17 -0
  23. data/lib/yeshua_crm/helpers/form_tag_helper.rb +123 -0
  24. data/lib/yeshua_crm/helpers/tags_helper.rb +13 -0
  25. data/lib/yeshua_crm/helpers/vote_helper.rb +35 -0
  26. data/lib/yeshua_crm/liquid/drops/cells_drop.rb +86 -0
  27. data/lib/yeshua_crm/liquid/drops/issues_drop.rb +66 -0
  28. data/lib/yeshua_crm/liquid/drops/news_drop.rb +54 -0
  29. data/lib/yeshua_crm/liquid/drops/users_drop.rb +72 -0
  30. data/lib/yeshua_crm/liquid/filters/arrays.rb +177 -0
  31. data/lib/yeshua_crm/liquid/filters/base.rb +208 -0
  32. data/lib/yeshua_crm/money_helper.rb +65 -0
  33. data/lib/yeshua_crm/version.rb +3 -0
  34. data/test/acts_as_draftable/draft_test.rb +29 -0
  35. data/test/acts_as_draftable/rcrm_acts_as_draftable_test.rb +185 -0
  36. data/test/acts_as_taggable/rcrm_acts_as_taggable_test.rb +345 -0
  37. data/test/acts_as_taggable/tag_list_test.rb +34 -0
  38. data/test/acts_as_taggable/tag_test.rb +72 -0
  39. data/test/acts_as_taggable/tagging_test.rb +15 -0
  40. data/test/acts_as_viewed/rcrm_acts_as_viewed_test.rb +47 -0
  41. data/test/acts_as_votable/rcrm_acts_as_votable_test.rb +19 -0
  42. data/test/acts_as_votable/rcrm_acts_as_voter_test.rb +14 -0
  43. data/test/acts_as_votable/votable_test.rb +507 -0
  44. data/test/acts_as_votable/voter_test.rb +296 -0
  45. data/test/currency_test.rb +292 -0
  46. data/test/liquid/drops/issues_drop_test.rb +34 -0
  47. data/test/liquid/drops/news_drop_test.rb +38 -0
  48. data/test/liquid/drops/projects_drop_test.rb +44 -0
  49. data/test/liquid/drops/uses_drop_test.rb +36 -0
  50. data/test/liquid/filters/arrays_filter_test.rb +24 -0
  51. data/test/liquid/filters/base_filter_test.rb +63 -0
  52. data/test/liquid/liquid_helper.rb +32 -0
  53. data/test/models/issue.rb +14 -0
  54. data/test/models/news.rb +3 -0
  55. data/test/models/project.rb +8 -0
  56. data/test/models/user.rb +11 -0
  57. data/test/models/vote_classes.rb +33 -0
  58. data/test/money_helper_test.rb +12 -0
  59. data/test/schema.rb +121 -0
  60. data/test/tags_helper_test.rb +29 -0
  61. data/test/test_helper.rb +66 -0
  62. data/test/vote_helper_test.rb +28 -0
  63. data/yeshua_crm.gemspec +28 -0
  64. metadata +206 -0
@@ -0,0 +1,65 @@
1
+ require 'action_view'
2
+
3
+ module YeshuaCrm
4
+ module MoneyHelper
5
+
6
+ def object_price(obj, price_field = :price, options = {})
7
+ options.merge!({:symbol => true})
8
+ price_to_currency(obj.try(price_field), obj.currency, options).to_s if obj.respond_to?(:currency)
9
+ end
10
+
11
+ def prices_collection_by_currency(prices_collection, options={})
12
+ return [] if prices_collection.blank? || prices_collection == 0
13
+ prices = prices_collection
14
+ prices.reject!{|k, v| v.to_i == 0} if options[:hide_zeros]
15
+ prices.collect{|k, v| content_tag(:span, price_to_currency(v, k, :symbol => true), :style => "white-space: nowrap;")}.compact
16
+ end
17
+
18
+ def deal_currency_icon(currency)
19
+ case currency.to_s.upcase
20
+ when 'EUR'
21
+ "icon-money-euro"
22
+ when 'GBP'
23
+ "icon-money-pound"
24
+ when 'JPY'
25
+ "icon-money-yen"
26
+ else
27
+ "icon-money-dollar"
28
+ end
29
+ end
30
+
31
+ def collection_for_currencies_select(default_currency = 'BRL', major_currencies = %w(BRL USD EUR GBP RUB CHF))
32
+ currencies = []
33
+ currencies << default_currency.to_s unless default_currency.blank?
34
+ currencies |= major_currencies
35
+ currencies.map do |c|
36
+ currency = YeshuaCrm::Currency.find(c)
37
+ ["#{currency.iso_code} (#{currency.symbol})", currency.iso_code] if currency
38
+ end.compact.uniq
39
+ end
40
+
41
+ def all_currencies
42
+ Currency.table.inject([]) do |array, (id, attributes)|
43
+ array ||= []
44
+ array << ["#{attributes[:name]}" + (attributes[:symbol].blank? ? "" : " (#{attributes[:symbol]})"), attributes[:iso_code]]
45
+ array
46
+ end.sort{|x, y| x[0] <=> y[0]}
47
+ end
48
+
49
+ def price_to_currency(price, currency="BRL", options={})
50
+ return '' if price.blank?
51
+
52
+ currency = "BRL" unless currency.is_a?(String)
53
+ currency = YeshuaCrm::Currency.find(currency)
54
+
55
+ return YeshuaCrm::Currency.format(price.to_f, currency, options)# if currency
56
+ price.to_s
57
+ end
58
+
59
+
60
+ end
61
+ end
62
+
63
+ unless ActionView::Base.included_modules.include?(YeshuaCrm::MoneyHelper)
64
+ ActionView::Base.send(:include, YeshuaCrm::MoneyHelper)
65
+ end
@@ -0,0 +1,3 @@
1
+ module YeshuaCrm
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ class DraftTest < ActiveSupport::TestCase
4
+ def test_restore
5
+ issue = Issue.new(subject: 'some subject', description: 'some description')
6
+
7
+ issue.save_draft(current_user)
8
+ restored_issue = YeshuaCrm::ActsAsDraftable::Draft.find(issue.draft_id).restore
9
+
10
+ assert_equal issue.subject, restored_issue.subject
11
+ assert_equal issue.description, restored_issue.description
12
+ end
13
+
14
+ def test_restore_all
15
+ first_issue = Issue.new(subject: 'first subject')
16
+ first_issue.save_draft(current_user)
17
+ second_issue = Issue.new(subject: 'second subject')
18
+ second_issue.save_draft(current_user)
19
+
20
+ restored_issues = YeshuaCrm::ActsAsDraftable::Draft.restore_all
21
+ assert_equal [first_issue.subject, second_issue.subject], restored_issues.map(&:subject).sort
22
+ end
23
+
24
+ private
25
+
26
+ def current_user
27
+ users(:sam)
28
+ end
29
+ end
@@ -0,0 +1,185 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ class RcrmActsAsDraftableTest < ActiveSupport::TestCase
4
+ def test_rcrm_acts_as_draftable_without_arguments
5
+ assert_nothing_raised do
6
+ Cell.rcrm_acts_as_draftable
7
+ end
8
+ end
9
+
10
+ class SomeClass < ActiveRecord::Base; end
11
+ def test_rcrm_acts_as_draftable_with_parent
12
+ assert_nothing_raised do
13
+ News.rcrm_acts_as_draftable(parent: :author)
14
+ end
15
+ assert User.new.respond_to?(:drafts)
16
+ end
17
+
18
+ def test_rcrm_acts_as_draftable_with_non_existing_parent
19
+ assert_raises do
20
+ Issue.rcrm_acts_as_draftable(parent: :foo)
21
+ end
22
+ end
23
+
24
+ def test_rcrm_acts_as_draftable_with_non_hash_argument
25
+ assert_raises do
26
+ Issue.rcrm_acts_as_draftable('bar')
27
+ end
28
+ end
29
+
30
+ def test_rcrm_acts_as_draftable_with_invalid_hash_key
31
+ assert_raises do
32
+ Issue.rcrm_acts_as_draftable(baz: 'qux')
33
+ end
34
+ end
35
+
36
+ def test_drafts
37
+ issue = Issue.new
38
+ issue.save_draft(current_user)
39
+
40
+ assert_equal 1, Issue.drafts(current_user).count
41
+ assert_equal YeshuaCrm::ActsAsDraftable::Draft, Issue.drafts(current_user).first.class
42
+ end
43
+
44
+ def test_from_draft
45
+ issue = Issue.new(subject: 'subject')
46
+ issue.save_draft(current_user)
47
+
48
+ issue_from_draft = Issue.from_draft(issue.draft_id)
49
+
50
+ assert_equal Issue, issue_from_draft.class
51
+ assert_equal true, issue_from_draft.new_record?
52
+ assert_equal issue.draft_id, issue_from_draft.draft_id
53
+
54
+ assert_equal issue.subject, issue_from_draft.subject
55
+ end
56
+
57
+ def test_draft_deleted_after_save
58
+ issue = Issue.new(subject: 'subject')
59
+ issue.save_draft(current_user)
60
+
61
+ issue_from_draft = Issue.from_draft(issue.draft_id)
62
+
63
+ assert_difference 'Issue.count', 1 do
64
+ assert_difference 'YeshuaCrm::ActsAsDraftable::Draft.count', -1 do
65
+ issue_from_draft.save!
66
+ end
67
+ end
68
+ assert_nil issue_from_draft.draft_id
69
+ end
70
+
71
+ def test_from_draft_with_non_existing_draft_id
72
+ assert_raises do
73
+ Issue.from_draft(999)
74
+ end
75
+ end
76
+
77
+ def test_from_draft_with_wrong_type
78
+ Cell.rcrm_acts_as_draftable
79
+
80
+ cell = Cell.new
81
+ cell.save_draft(current_user)
82
+
83
+ assert_raises do
84
+ Issue.from_draft(cell.draft_id)
85
+ end
86
+ end
87
+
88
+ def test_dump_to_draft
89
+ assert_equal String, Issue.new.dump_to_draft.class
90
+ end
91
+
92
+ def test_load_to_draft
93
+ attributes = {subject: 'subject', description: 'description'}
94
+ draft = Issue.new(attributes).dump_to_draft
95
+
96
+ issue = Issue.new
97
+ issue.load_from_draft(draft)
98
+
99
+ assert_equal attributes[:subject], issue.subject
100
+ assert_equal attributes[:description], issue.description
101
+ end
102
+
103
+ def test_save_draft
104
+ attributes = {subject: 'subject'}
105
+ issue = Issue.new(attributes)
106
+
107
+ assert_difference 'Issue.count', 0 do
108
+ assert_difference 'YeshuaCrm::ActsAsDraftable::Draft.count', 1 do
109
+ result = issue.save_draft(current_user)
110
+ assert_equal true, result
111
+ end
112
+ end
113
+ assert_equal Fixnum, issue.draft_id.class
114
+
115
+
116
+ draft = YeshuaCrm::ActsAsDraftable::Draft.find(issue.draft_id)
117
+ assert_equal 'Issue', draft.target_type
118
+ assert_equal current_user.id, draft.user_id
119
+ assert_equal attributes[:subject], draft.restore.subject
120
+ end
121
+
122
+ def test_save_draft_without_user
123
+ issue = Issue.new
124
+
125
+ assert_difference 'Issue.count', 0 do
126
+ assert_difference 'YeshuaCrm::ActsAsDraftable::Draft.count', 1 do
127
+ result = issue.save_draft
128
+ assert_equal true, result
129
+ end
130
+ end
131
+
132
+ draft = YeshuaCrm::ActsAsDraftable::Draft.find(issue.draft_id)
133
+ assert_nil draft.user_id
134
+ end
135
+
136
+ def test_save_draft_with_associations
137
+ issue = Issue.new(cell: cells(:second_cell))
138
+ issue.save_draft(current_user)
139
+
140
+ draft = YeshuaCrm::ActsAsDraftable::Draft.find(issue.draft_id)
141
+ assert_equal issue.cell.name, draft.restore.cell.name
142
+ end
143
+
144
+ def test_save_draft_updates_existing_draft
145
+ issue = Issue.new
146
+ issue.save_draft(current_user)
147
+
148
+ issue.subject = 'changed subject'
149
+ assert_difference 'Issue.count', 0 do
150
+ assert_difference 'YeshuaCrm::ActsAsDraftable::Draft.count', 0 do
151
+ issue.save_draft(current_user)
152
+ end
153
+ end
154
+
155
+ draft = YeshuaCrm::ActsAsDraftable::Draft.find(issue.draft_id)
156
+ assert_equal issue.subject, draft.restore.subject
157
+ end
158
+
159
+ def test_save_draft_does_not_save_persisted_model
160
+ issue = Issue.new
161
+ issue.save_draft(current_user)
162
+ issue.save!
163
+ assert_equal false, issue.save_draft(current_user)
164
+ end
165
+
166
+ def test_update_draft
167
+ issue = Issue.new(subject: 'subject')
168
+ issue.save_draft(current_user)
169
+
170
+ assert_difference 'Issue.count', 0 do
171
+ assert_difference 'YeshuaCrm::ActsAsDraftable::Draft.count', 0 do
172
+ issue.update_draft(current_user, subject: 'updated_subject')
173
+ end
174
+ end
175
+
176
+ draft = YeshuaCrm::ActsAsDraftable::Draft.find(issue.draft_id)
177
+ assert_equal issue.subject, draft.restore.subject
178
+ end
179
+
180
+ private
181
+
182
+ def current_user
183
+ users(:sam)
184
+ end
185
+ end
@@ -0,0 +1,345 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class RcrmActsAsTaggableTest < ActiveSupport::TestCase
4
+ def test_available_tags
5
+ assert_equivalent [tags(:feature), tags(:bug), tags(:error), tags(:question)], Issue.available_tags(Cell.first)
6
+ assert_equivalent [tags(:error), tags(:question)], Issue.available_tags(:cell => Cell.first, :limit => 2)
7
+ end
8
+
9
+ def test_find_related_tags_with
10
+ assert_equivalent [tags(:feature), tags(:bug), tags(:question)], Issue.find_related_tags("error")
11
+ assert_equivalent [tags(:feature), tags(:error), tags(:question)], Issue.find_related_tags(tags(:bug))
12
+ assert_equivalent [tags(:error), tags(:question)], Issue.find_related_tags(["New feature", "bug"])
13
+ assert_equivalent [tags(:feature), tags(:bug)], Issue.find_related_tags([tags(:error), tags(:question)])
14
+ end
15
+
16
+ def test_find_tagged_with_include_and_order
17
+ assert_equal issues(:third_issue, :first_issue, :second_issue), Issue.find_tagged_with("question", :order => "issues.description DESC", :include => :user).to_a
18
+ end
19
+
20
+ def test_find_related_tags_with_non_existent_tags
21
+ assert_equal [], Issue.find_related_tags("ABCDEFG")
22
+ assert_equal [], Issue.find_related_tags(['HIJKLM'])
23
+ end
24
+
25
+ def test_find_related_tags_with_nothing
26
+ assert_equal [], Issue.find_related_tags("")
27
+ assert_equal [], Issue.find_related_tags([])
28
+ end
29
+
30
+ def test_find_tagged_with
31
+ assert_equivalent [issues(:first_issue), issues(:second_issue), issues(:third_issue)], Issue.find_tagged_with('"error"')
32
+ assert_equal Issue.find_tagged_with('"error"'), Issue.find_tagged_with(['error'])
33
+ assert_equal Issue.find_tagged_with('"error"'), Issue.find_tagged_with([tags(:error)])
34
+
35
+ assert_equivalent [issues(:second_issue),], Issue.find_tagged_with('New feature')
36
+ assert_equal Issue.find_tagged_with('New feature'), Issue.find_tagged_with(['New feature'])
37
+ assert_equal Issue.find_tagged_with('New feature'), Issue.find_tagged_with([tags(:feature)])
38
+ end
39
+
40
+ def test_find_tagged_with_nothing
41
+ assert_equal [], Issue.find_tagged_with("")
42
+ assert_equal [], Issue.find_tagged_with([])
43
+ end
44
+
45
+ def test_find_tagged_with_nonexistant_tags
46
+ assert_equal [], Issue.find_tagged_with('ABCDEFG')
47
+ assert_equal [], Issue.find_tagged_with(['HIJKLM'])
48
+ assert_equal [], Issue.find_tagged_with([YeshuaCrm::ActsAsTaggable::Tag.new(:name => 'unsaved tag')])
49
+ end
50
+
51
+ def test_find_tagged_with_match_all
52
+ assert_equivalent [issues(:second_issue)], Issue.find_tagged_with('error, "bug", "New feature", "question"', :match_all => true)
53
+ end
54
+
55
+ def test_find_tagged_with_match_all_and_include
56
+ assert_equivalent [issues(:first_issue), issues(:second_issue), issues(:third_issue)], Issue.find_tagged_with(['error', 'question'], :match_all => true, :include => :tags)
57
+ end
58
+
59
+ def test_find_tagged_with_conditions
60
+ assert_equal [], Issue.find_tagged_with('"error", bug', :conditions => '1=0')
61
+ end
62
+
63
+ def test_find_tagged_with_duplicates_options_hash
64
+ options = { :conditions => '1=1' }.freeze
65
+ assert_nothing_raised { Issue.find_tagged_with("error", options) }
66
+ end
67
+
68
+ def test_find_tagged_with_exclusions
69
+ assert_equivalent [issues(:first_issue), issues(:third_issue)], Issue.find_tagged_with("bug", :exclude => true)
70
+ assert_equivalent [issues(:first_issue), issues(:third_issue)], Issue.find_tagged_with("'bug', feature", :exclude => true)
71
+ end
72
+
73
+ def test_find_options_for_find_tagged_with_no_tags_returns_empty_hash
74
+ assert_equal Hash.new, Issue.find_options_for_find_tagged_with("")
75
+ assert_equal Hash.new, Issue.find_options_for_find_tagged_with([nil])
76
+ end
77
+
78
+ def test_find_options_for_find_tagged_with_leaves_arguments_unchanged
79
+ original_tags = issues(:second_issue).tags.dup
80
+ Issue.find_options_for_find_tagged_with(issues(:second_issue).tags)
81
+ assert_equal original_tags, issues(:second_issue).tags
82
+ end
83
+
84
+ def test_find_options_for_find_tagged_with_respects_custom_table_name
85
+ YeshuaCrm::ActsAsTaggable::Tagging.table_name = "categorisations"
86
+ YeshuaCrm::ActsAsTaggable::Tag.table_name = "categories"
87
+
88
+ options = Issue.find_options_for_find_tagged_with("Hello")
89
+
90
+ assert_no_match(/ taggings /, options[:joins])
91
+ assert_no_match(/ tags /, options[:joins])
92
+
93
+ assert_match(/ categorisations /, options[:joins])
94
+ assert_match(/ categories /, options[:joins])
95
+ ensure
96
+ YeshuaCrm::ActsAsTaggable::Tagging.table_name = "taggings"
97
+ YeshuaCrm::ActsAsTaggable::Tag.table_name = "tags"
98
+ end
99
+
100
+ def test_include_tags_on_find_tagged_with
101
+ assert_nothing_raised do
102
+ Issue.find_tagged_with('error', :include => :tags)
103
+ Issue.find_tagged_with("error", :include => { :taggings => :tag })
104
+ end
105
+ end
106
+
107
+ def test_basic_tag_counts_on_class
108
+ assert_tag_counts Issue.tag_counts, :error => 3, :feature => 1, :question => 3, :bug => 1
109
+ end
110
+
111
+ def test_tag_counts_on_class_with_date_conditions
112
+ assert_tag_counts Issue.tag_counts(:start_at => Date.new(2015, 1, 1)), :error => 2, :feature => 1, :question => 3, :bug => 1
113
+ assert_tag_counts Issue.tag_counts(:end_at => Date.new(2014, 12, 31)), :error => 1
114
+ assert_tag_counts Issue.tag_counts(:start_at => Date.new(2015, 1, 31), :end_at => Date.new(2015, 3, 1)), :question => 1
115
+ end
116
+
117
+ def test_tag_counts_on_class_with_frequencies
118
+ assert_tag_counts Issue.tag_counts(:at_least => 2), :question => 3, :error => 3
119
+ assert_tag_counts Issue.tag_counts(:at_most => 2), :bug => 1, :feature => 1
120
+ end
121
+
122
+ def test_tag_counts_on_class_with_frequencies_and_conditions
123
+ assert_tag_counts Issue.tag_counts(:at_least => 2, :conditions => '1=1'), :question => 3, :error => 3
124
+ end
125
+
126
+ def test_tag_counts_duplicates_options_hash
127
+ options = { :at_least => 2, :conditions => '1=1' }.freeze
128
+ assert_nothing_raised { Issue.tag_counts(options) }
129
+ end
130
+
131
+ def test_tag_counts_with_limit
132
+ assert_equal 2, Issue.tag_counts(:limit => 2).to_a.size
133
+ assert_equal 2, Issue.tag_counts(:at_least => 3, :limit => 2).to_a.size
134
+ end
135
+
136
+ def test_tag_counts_with_limit_and_order
137
+ assert_equivalent YeshuaCrm::ActsAsTaggable::Tag.where(:id => [tags(:error), tags(:question)]), Issue.tag_counts(:order => 'count desc', :limit => 2)
138
+ end
139
+
140
+ def test_tag_counts_on_association
141
+ assert_tag_counts users(:jonathan).issues.tag_counts, :error => 2, :bug => 1, :question => 2, :feature => 1
142
+ assert_tag_counts users(:sam).issues.tag_counts, :error => 1, :question => 1
143
+ end
144
+
145
+ def test_tag_counts_on_association_with_options
146
+ assert_equal [], users(:jonathan).issues.tag_counts(:conditions => '1=0')
147
+ assert_tag_counts users(:jonathan).issues.tag_counts(:at_most => 2), :bug => 1,
148
+ :feature => 1, :error => 2, :question => 2
149
+ end
150
+
151
+ def test_tag_counts_on_model_instance
152
+ assert_tag_counts issues(:third_issue).tag_counts, :error => 3, :question => 3
153
+ end
154
+
155
+ def test_tag_counts_on_model_instance_merges_conditions
156
+ assert_tag_counts issues(:first_issue).tag_counts(:conditions => "tags.name = 'error'"), :error => 3
157
+ end
158
+
159
+ def test_tag_counts_on_model_instance_with_no_tags
160
+ issue = Issue.create!(:description => "desc")
161
+
162
+ assert_tag_counts issue.tag_counts, {}
163
+ end
164
+
165
+ def test_tag_counts_should_sanitize_scope_conditions
166
+ Issue.send :where, { "tags.id = ?" => tags(:error).id } do
167
+ assert_tag_counts Issue.tag_counts, :error => 3
168
+ end
169
+ end
170
+
171
+ def test_tag_counts_respects_custom_table_names
172
+ YeshuaCrm::ActsAsTaggable::Tagging.table_name = "categorisations"
173
+ YeshuaCrm::ActsAsTaggable::Tag.table_name = "categories"
174
+
175
+ options = Issue.find_options_for_tag_counts(:start_at => 2.weeks.ago, :end_at => Date.today)
176
+ sql = options.values.join(' ')
177
+
178
+ assert_no_match %r{taggings}, sql
179
+ assert_no_match %r{tags}, sql
180
+
181
+ assert_match /categorisations/, sql
182
+ assert_match /categories/, sql
183
+ ensure
184
+ YeshuaCrm::ActsAsTaggable::Tagging.table_name = "taggings"
185
+ YeshuaCrm::ActsAsTaggable::Tag.table_name = "tags"
186
+ end
187
+
188
+ def test_tag_list_reader
189
+ assert_equivalent ["error", "question"], issues(:first_issue).tag_list
190
+ assert_equivalent ["error", "New feature", "bug", "question"], issues(:second_issue).tag_list
191
+ end
192
+
193
+ def test_reassign_tag_list
194
+ assert_equivalent ["error", "question"], issues(:first_issue).tag_list
195
+ issues(:first_issue).taggings.reload
196
+
197
+ # Only an update of the issues table should be executed, the other two queries are for savepoints
198
+ # assert_queries 3 do
199
+ # issues(:first_issue).update_attributes!(:description => "new name", :tag_list => issues(:first_issue).tag_list.to_s)
200
+ # end
201
+
202
+ assert_equivalent ["error", "question"], issues(:first_issue).tag_list
203
+ end
204
+
205
+ def test_new_tags
206
+ assert_equivalent ["error", "question"], issues(:first_issue).tag_list
207
+ issues(:first_issue).update_attributes!(:tag_list => "#{issues(:first_issue).tag_list}, One, Two")
208
+ assert_equivalent ["error", "question", "One", "Two"], issues(:first_issue).tag_list
209
+ end
210
+
211
+ def test_remove_tag
212
+ assert_equivalent ["error", "question"], issues(:first_issue).tag_list
213
+ issues(:first_issue).update_attributes!(:tag_list => "error")
214
+ assert_equivalent ["error"], issues(:first_issue).tag_list
215
+ end
216
+
217
+ def test_remove_and_add_tag
218
+ assert_equivalent ["error", "question"], issues(:first_issue).tag_list
219
+ issues(:first_issue).update_attributes!(:tag_list => "question, Beautiful")
220
+ assert_equivalent ["question", "Beautiful"], issues(:first_issue).tag_list
221
+ end
222
+
223
+ def test_tags_not_saved_if_validation_fails
224
+ issue = issues(:first_issue)
225
+ assert_equivalent ["error", "question"], issue.tag_list
226
+
227
+ issue.stub(:valid?, false) do
228
+ assert !issue.update_attributes(tag_list: "One, Two")
229
+ end
230
+ assert_equivalent ["error", "question"], Issue.find(issue.id).tag_list
231
+ end
232
+
233
+ def test_tag_list_accessors_on_new_record
234
+ p = Issue.new(:description => 'Test')
235
+
236
+ assert p.tag_list.blank?
237
+ p.tag_list = "One, Two"
238
+ assert_equal "One, Two", p.tag_list.to_s
239
+ end
240
+
241
+ def test_clear_tag_list_with_nil
242
+ p = issues(:second_issue)
243
+
244
+ assert !p.tag_list.blank?
245
+ assert p.update_attributes(:tag_list => nil)
246
+ assert p.tag_list.blank?
247
+
248
+ assert p.reload.tag_list.blank?
249
+ end
250
+
251
+ def test_clear_tag_list_with_string
252
+ p = issues(:second_issue)
253
+
254
+ assert !p.tag_list.blank?
255
+ assert p.update_attributes(:tag_list => ' ')
256
+ assert p.tag_list.blank?
257
+
258
+ assert p.reload.tag_list.blank?
259
+ end
260
+
261
+ def test_tag_list_reset_on_reload
262
+ p = issues(:second_issue)
263
+ assert !p.tag_list.blank?
264
+ p.tag_list = nil
265
+ assert p.tag_list.blank?
266
+ assert !p.reload.tag_list.blank?
267
+ end
268
+
269
+ def test_instance_tag_counts
270
+ assert_tag_counts issues(:first_issue).tag_counts, :error => 3, :question => 3
271
+ end
272
+
273
+ def test_tag_list_populated_when_cache_nil
274
+ assert_nil issues(:first_issue).cached_tag_list
275
+ issues(:first_issue).save!
276
+ assert_equal issues(:first_issue).tag_list.to_s, issues(:first_issue).cached_tag_list
277
+ end
278
+
279
+ def test_cached_tag_list_updated
280
+ assert_nil issues(:first_issue).cached_tag_list
281
+ issues(:first_issue).save!
282
+ assert_equivalent ["question", "error"], YeshuaCrm::ActsAsTaggable::TagList.from(issues(:first_issue).cached_tag_list)
283
+ issues(:first_issue).update_attributes!(:tag_list => "None")
284
+
285
+ assert_equal 'None', issues(:first_issue).cached_tag_list
286
+ assert_equal 'None', issues(:first_issue).reload.cached_tag_list
287
+ end
288
+
289
+ def test_clearing_cached_tag_list
290
+ # Generate the cached tag list
291
+ issues(:first_issue).save!
292
+
293
+ issues(:first_issue).update_attributes!(:tag_list => "")
294
+ assert_equal "", issues(:first_issue).cached_tag_list
295
+ end
296
+
297
+ def test_find_tagged_with_using_sti
298
+ issue = Issue.create!(description: 'Test', tag_list: 'Random')
299
+ assert_equal [issue], Issue.find_tagged_with('Random')
300
+ end
301
+
302
+ def test_case_insensitivity
303
+ assert_difference "YeshuaCrm::ActsAsTaggable::Tag.count", 1 do
304
+ Issue.create!(:description => "Test", :tag_list => "one")
305
+ Issue.create!(:description => "Test", :tag_list => "One")
306
+ end
307
+ assert_equal Issue.find_tagged_with("question"), Issue.find_tagged_with("question")
308
+ end
309
+
310
+ def test_tag_not_destroyed_when_unused
311
+ issues(:first_issue).tag_list.add("Random")
312
+ issues(:first_issue).save!
313
+
314
+ assert_no_difference 'YeshuaCrm::ActsAsTaggable::Tag.count' do
315
+ issues(:first_issue).tag_list.remove("Random")
316
+ issues(:first_issue).save!
317
+ end
318
+ end
319
+
320
+ def test_tag_destroyed_when_unused
321
+ YeshuaCrm::ActsAsTaggable::Tag.destroy_unused = true
322
+
323
+ issues(:first_issue).tag_list.add("Random")
324
+ issues(:first_issue).save!
325
+
326
+ assert_difference 'YeshuaCrm::ActsAsTaggable::Tag.count', -1 do
327
+ issues(:first_issue).tag_list.remove("Random")
328
+ issues(:first_issue).save!
329
+ end
330
+ ensure
331
+ YeshuaCrm::ActsAsTaggable::Tag.destroy_unused = false
332
+ end
333
+
334
+ def test_tags_condition
335
+ assert_equal "(tags_TABLE.name LIKE #{tags(:feature).id} OR tags_TABLE.name LIKE #{tags(:bug).id})",
336
+ Issue.send(:tags_condition, [tags(:feature), tags(:bug)], 'tags_TABLE')
337
+ end
338
+
339
+ def test_all_tags_list
340
+ issues(:first_issue).tag_list.remove('error')
341
+ issues(:first_issue).tag_list.add('new')
342
+ issues(:first_issue).save!
343
+ assert_equal %w(question new), issues(:first_issue).reload.all_tags_list
344
+ end
345
+ end
@@ -0,0 +1,34 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class TagListTest < ActiveSupport::TestCase
4
+ def setup
5
+ @tag_list = YeshuaCrm::ActsAsTaggable::TagList.new(%w(error bug))
6
+ end
7
+
8
+ def test_from
9
+ assert_equal %w(one two three), YeshuaCrm::ActsAsTaggable::TagList.from('one, two, two, three, three, three')
10
+ end
11
+
12
+ def test_add
13
+ @tag_list.add(['new_tag'])
14
+ assert_equal %w(error bug new_tag), @tag_list
15
+ end
16
+
17
+ def test_remove
18
+ @tag_list.remove(['old_tag'])
19
+ assert_equal %w(error bug), @tag_list
20
+ @tag_list.remove(['error'])
21
+ assert_equal %w(bug), @tag_list
22
+ end
23
+
24
+ def test_toggle
25
+ @tag_list.toggle(['new_tag'])
26
+ assert_equal %w(error bug new_tag), @tag_list
27
+ @tag_list.toggle(['error'])
28
+ assert_equal %w(bug new_tag), @tag_list
29
+ end
30
+
31
+ def test_to_s
32
+ assert_equal 'error, bug', @tag_list.to_s
33
+ end
34
+ end