ccls-common_lib 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/README.rdoc +116 -0
  3. data/lib/ccls-common_lib.rb +1 -0
  4. data/lib/common_lib.rb +37 -0
  5. data/lib/common_lib/action_controller_extension.rb +8 -0
  6. data/lib/common_lib/action_controller_extension/accessible_via_protocol.rb +405 -0
  7. data/lib/common_lib/action_controller_extension/accessible_via_user.rb +605 -0
  8. data/lib/common_lib/action_controller_extension/routing.rb +20 -0
  9. data/lib/common_lib/action_controller_extension/test_case.rb +22 -0
  10. data/lib/common_lib/action_view_extension.rb +3 -0
  11. data/lib/common_lib/action_view_extension/base.rb +310 -0
  12. data/lib/common_lib/action_view_extension/form_builder.rb +197 -0
  13. data/lib/common_lib/active_model.rb +4 -0
  14. data/lib/common_lib/active_model/errors.rb +16 -0
  15. data/lib/common_lib/active_model/validations/absence.rb +78 -0
  16. data/lib/common_lib/active_model/validations/complete_date.rb +138 -0
  17. data/lib/common_lib/active_model/validations/past_date.rb +121 -0
  18. data/lib/common_lib/active_record.rb +1 -0
  19. data/lib/common_lib/active_record/base.rb +129 -0
  20. data/lib/common_lib/active_support_extension.rb +12 -0
  21. data/lib/common_lib/active_support_extension/assertions.rb +108 -0
  22. data/lib/common_lib/active_support_extension/associations.rb +154 -0
  23. data/lib/common_lib/active_support_extension/attributes.rb +296 -0
  24. data/lib/common_lib/active_support_extension/pending.rb +115 -0
  25. data/lib/common_lib/active_support_extension/test_case.rb +203 -0
  26. data/lib/common_lib/railtie.rb +48 -0
  27. data/lib/common_lib/ruby.rb +8 -0
  28. data/lib/common_lib/ruby/array.rb +128 -0
  29. data/lib/common_lib/ruby/fixnum.rb +5 -0
  30. data/lib/common_lib/ruby/hash.rb +51 -0
  31. data/lib/common_lib/ruby/integer.rb +11 -0
  32. data/lib/common_lib/ruby/nil_class.rb +13 -0
  33. data/lib/common_lib/ruby/numeric.rb +0 -0
  34. data/lib/common_lib/ruby/object.rb +53 -0
  35. data/lib/common_lib/ruby/string.rb +20 -0
  36. data/lib/common_lib/translation_table.rb +129 -0
  37. data/lib/tasks/common_lib.rake +10 -0
  38. data/lib/tasks/csv.rake +0 -0
  39. data/lib/tasks/database.rake +229 -0
  40. data/lib/tasks/rcov.rake +52 -0
  41. data/vendor/assets/javascripts/common_lib.js +77 -0
  42. data/vendor/assets/stylesheets/common_lib.css +71 -0
  43. metadata +84 -0
@@ -0,0 +1,115 @@
1
+ # Some code from jeremymcanally's "pending"
2
+ # https://github.com/jeremymcanally/pending/tree/master
3
+
4
+ module ActiveSupport
5
+ module Testing
6
+ module Pending
7
+
8
+ unless defined?(Spec)
9
+
10
+ @@pending_cases = []
11
+ @@at_exit = false
12
+
13
+ def pending(description = "No description given", &block)
14
+ # just because its defined doesn't mean that the skip method exists
15
+ # this should be is_a?(MiniTest::Unit::TestCase)
16
+ # if defined?(::MiniTest)
17
+ # if defined?(::MiniTest) and self.is_a?(::MiniTest::Unit::TestCase)
18
+ # skip(description.blank? ? nil : description)
19
+ # else
20
+ if description.is_a?(Symbol)
21
+ is_pending = $tags[description]
22
+ return block.call unless is_pending
23
+ end
24
+
25
+ if block_given?
26
+ failed = false
27
+
28
+ begin
29
+ block.call
30
+ rescue Exception
31
+ failed = true
32
+ end
33
+
34
+ flunk("<#{description}> did not fail.") unless failed
35
+ end
36
+
37
+ # The original
38
+ # caller[0] =~ (/(.*):(.*):in `(.*)'/)
39
+ # @@pending_cases << "#{$3} at #{$1}, line #{$2}"
40
+ #
41
+
42
+ # Old notes
43
+ # caller[0] =~ (/(.*):(.*):in `(.*)'/)
44
+ # @@pending_cases << "#{$3} at #{$1}, line #{$2}"
45
+ #
46
+ ## caller[0] will be like ...
47
+ ## "/Users/jakewendt/github_repo/ccls/odms/test/unit/sample_test.rb:95:in `block in <class:SampleTest>'"
48
+ # # looks like we lose the name of the 'method' in 1.9.1
49
+ #
50
+ # # Gotta remember these as the next Regex will overwrite them.
51
+ # filename = $1
52
+ # linenumber = $2
53
+ #
54
+ ### ruby 1.8.7
55
+ ### Hx/Addresses Controller should NOT create new address with employee login and invalid address:
56
+ ### ruby 1.9.1
57
+ ###Hx/Addresses Controller block (2 levels) in <class:AddressesControllerTest>:
58
+ #
59
+ ## testmethod = $3
60
+ ## model = self.class.to_s.gsub(/Test$/,'').titleize
61
+ ## method = testmethod.gsub(/_/,' ').gsub(/^test /,'')
62
+ ### @@pending_cases << "#{model} #{method}:\n.\t#{filename} line #{linenumber}"
63
+ # @@pending_cases << "#{filename} line #{linenumber}"
64
+ ### @@pending_cases << "#{testmethod} at #{filename}, line #{linenumber}"
65
+ #
66
+
67
+ # "/Users/jakewendt/github_repo/ccls/odms/test/unit/sample_test.rb:95:in `block in <class:SampleTest>'"
68
+ caller[0] =~ (/(.*):(.*):in `(.*)'/)
69
+
70
+ # self.to_s => test_should_be_pending(RoleTest)
71
+
72
+ # @@pending_cases << "#{$3} at #{$1}, line #{$2}"
73
+ # @@pending_cases << ["#{self.to_s} [#{$1}:#{$2}]",description]
74
+ @@pending_cases << [self.to_s,"[#{$1}:#{$2}]",description]
75
+
76
+ # 1) Skipped:
77
+ #test_should_not_update_attribute_if_new_data_is_blank(BcInfoTest) [/opt/local/lib/ruby1.9/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/testing/pending.rb:15]:
78
+ #Skipped, no message given
79
+ #
80
+ # 2) Skipped:
81
+ #test_should_send_save_failed_notification_if_subject_changed_and_save_failed(BcInfoTest) [/opt/local/lib/ruby1.9/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/testing/pending.rb:15]:
82
+ #Skipped, no message given
83
+
84
+
85
+ # making the output look more similar to "skip"
86
+ # probably should reincorporate this into CommonLib
87
+
88
+ print "P"
89
+
90
+ @@at_exit ||= begin
91
+ at_exit do
92
+ puts "\nPending Cases:"
93
+ # @@pending_cases.each do |test_case|
94
+ @@pending_cases.each_with_index do |test_case,i|
95
+ # puts " #{i+1}) Pending:"
96
+ printf "%4d) Pending:\n", i+1
97
+ puts test_case
98
+ # if i just puts the array it will append carriage returns after each element
99
+ # puts test_case[0]
100
+ # puts test_case[1]
101
+ puts
102
+ end
103
+ puts
104
+ end
105
+ end
106
+
107
+ # end # if defined?(::MiniTest)
108
+
109
+ end # def pending(description = "", &block)
110
+
111
+ end # unless defined?(Spec)
112
+
113
+ end # module Pending
114
+ end # module Testing
115
+ end # module ActiveSupport
@@ -0,0 +1,203 @@
1
+ module CommonLib::ActiveSupportExtension::TestCase
2
+
3
+ def self.included(base)
4
+ # basically to include the model_name method to the CLASS
5
+ base.extend ActiveModel::Naming
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+ #
11
+ # this used to be called st_model_name, which I think
12
+ # was short for simply_testable_model_name. Of course,
13
+ # I left no description
14
+ #
15
+ def model_name_without_test
16
+ self.name.demodulize.sub(/Test$/,'')
17
+ end
18
+
19
+ def assert_should_act_as_list(*args)
20
+ options = args.extract_options!
21
+ scope = options[:scope]
22
+
23
+ test "#{brand}should act as list" do
24
+ object = create_object
25
+ first_position = object.position
26
+ assert first_position > 0
27
+ attrs = {}
28
+ Array(scope).each do |attr|
29
+ attrs[attr.to_sym] = object.send(attr)
30
+ end if scope
31
+ object = create_object(attrs)
32
+ assert_equal ( first_position + 1 ), object.position
33
+ object = create_object(attrs)
34
+ assert_equal ( first_position + 2 ), object.position
35
+ end
36
+
37
+ end
38
+
39
+ #
40
+ #
41
+ # What? No assert_requires_absence method???
42
+ # Its usually conditional so would be pretty pointless
43
+ #
44
+
45
+ def assert_requires_past_date(*attr_names)
46
+ options = { :allow_today => true }
47
+ options.update(attr_names.extract_options!)
48
+ model = options[:model] || model_name_without_test
49
+
50
+ attr_names.each do |attr_name|
51
+ if options[:allow_today]
52
+ test "#{brand}should allow #{attr_name} to be today" do
53
+ object = model.constantize.new
54
+ object.send("#{attr_name}=", Date.current)
55
+ object.valid? # could be, but probably isn't
56
+ assert !object.errors.matching?(attr_name,
57
+ 'is in the future and must be in the past'),
58
+ "Expected #{attr_name}:is NOT not a past date, but only got " <<
59
+ object.errors.full_messages.to_sentence
60
+ end
61
+ else
62
+ test "#{brand}should NOT allow #{attr_name} to be today" do
63
+ object = model.constantize.new
64
+ object.send("#{attr_name}=", Date.current)
65
+ assert !object.valid?
66
+ assert object.errors.matching?(attr_name,
67
+ 'is in the future and must be in the past'),
68
+ "Expected #{attr_name}:is not a past date, but only got " <<
69
+ object.errors.full_messages.to_sentence
70
+ end
71
+ end
72
+ test "#{brand}should require Date #{attr_name} be in the past" do
73
+ object = model.constantize.new
74
+ object.send("#{attr_name}=", Date.yesterday)
75
+ object.valid? # could be, but probably isn't
76
+ assert !object.errors.matching?(attr_name,
77
+ 'is in the future and must be in the past'),
78
+ "Expected #{attr_name}:is NOT not a past date, but only got " <<
79
+ object.errors.full_messages.to_sentence
80
+
81
+ object = model.constantize.new
82
+ object.send("#{attr_name}=", Date.tomorrow)
83
+ assert !object.valid?
84
+ assert object.errors.matching?(attr_name,
85
+ 'is in the future and must be in the past'),
86
+ "Expected #{attr_name}:is not a past date, but only got " <<
87
+ object.errors.full_messages.to_sentence
88
+ end
89
+ # doesn't seem to actually compare as a DateTime
90
+ test "#{brand}should require DateTime #{attr_name} be in the past" do
91
+ object = model.constantize.new
92
+ object.send("#{attr_name}=", (DateTime.current - 1.day))
93
+ object.valid? # could be, but probably isn't
94
+ assert !object.errors.matching?(attr_name,
95
+ 'is in the future and must be in the past'),
96
+ "Expected #{attr_name}:is NOT not a past date, but only got " <<
97
+ object.errors.full_messages.to_sentence
98
+
99
+ object = model.constantize.new
100
+ object.send("#{attr_name}=", (DateTime.current + 1.day))
101
+ assert !object.valid?
102
+ assert object.errors.matching?(attr_name,
103
+ 'is in the future and must be in the past'),
104
+ "Expected #{attr_name}:is not a past date, but only got " <<
105
+ object.errors.full_messages.to_sentence
106
+ end
107
+ test "#{brand}should require ActiveSupport::TimeWithZone #{attr_name} be in the past" do
108
+ object = model.constantize.new
109
+ object.send("#{attr_name}=", (Time.zone.now - 1.day))
110
+ object.valid? # could be, but probably isn't
111
+ assert !object.errors.matching?(attr_name,
112
+ 'is in the future and must be in the past'),
113
+ "Expected #{attr_name}:is NOT not a past date, but only got " <<
114
+ object.errors.full_messages.to_sentence
115
+
116
+ object = model.constantize.new
117
+ object.send("#{attr_name}=", (Time.zone.now + 1.day))
118
+ assert !object.valid?
119
+ assert object.errors.matching?(attr_name,
120
+ 'is in the future and must be in the past'),
121
+ "Expected #{attr_name}:is not a past date, but only got " <<
122
+ object.errors.full_messages.to_sentence
123
+ end
124
+ end
125
+ end
126
+
127
+
128
+ def assert_requires_complete_date(*attr_names)
129
+ options = attr_names.extract_options!
130
+ model = options[:model] || model_name_without_test
131
+
132
+ attr_names.each do |attr_name|
133
+ test "#{brand}should require a complete date for #{attr_name}" do
134
+ object = model.constantize.new
135
+ object.send("#{attr_name}=", "Sept 11, 2001")
136
+ object.valid? # could be, but probably isn't
137
+ assert !object.errors.matching?(attr_name,'is not a complete date'),
138
+ "Expected #{attr_name}:is NOT not a complete date, but only got " <<
139
+ object.errors.full_messages.to_sentence
140
+
141
+ object = model.constantize.new
142
+ object.send("#{attr_name}=", "Sept 2001")
143
+ assert !object.valid?
144
+ assert object.errors.matching?(attr_name,'is not a complete date'),
145
+ "Expected #{attr_name}:is not a complete date, but only got " <<
146
+ object.errors.full_messages.to_sentence
147
+
148
+ object = model.constantize.new
149
+ object.send("#{attr_name}=", "9/2001")
150
+ assert !object.valid?
151
+ assert object.errors.matching?(attr_name,'is not a complete date')
152
+ "Expected #{attr_name}:is not a complete date, but only got " <<
153
+ object.errors.full_messages.to_sentence
154
+ end
155
+ end
156
+ end
157
+
158
+ end
159
+
160
+
161
+ at_exit {
162
+ puts Dir.pwd()
163
+ puts Time.now
164
+ }
165
+
166
+ # basically a copy of assert_difference, but
167
+ # without any explicit comparison as it is
168
+ # simply stating that something will change
169
+ # (designed for updated_at)
170
+ def assert_changes(expression, message = nil, &block)
171
+ b = block.send(:binding)
172
+ exps = Array.wrap(expression)
173
+ before = exps.map { |e| eval(e, b) }
174
+ yield
175
+ exps.each_with_index do |e, i|
176
+ error = "#{e.inspect} didn't change"
177
+ error = "#{message}.\n#{error}" if message
178
+ assert_not_equal(before[i], eval(e, b), error)
179
+ end
180
+ end
181
+
182
+ # Just a negation of assert_changes
183
+ def deny_changes(expression, message = nil, &block)
184
+ b = block.send(:binding)
185
+ exps = Array.wrap(expression)
186
+ before = exps.map { |e| eval(e, b) }
187
+ yield
188
+ exps.each_with_index do |e, i|
189
+ error = "#{e.inspect} changed"
190
+ error = "#{message}.\n#{error}" if message
191
+ assert_equal(before[i], eval(e, b), error)
192
+ end
193
+ end
194
+
195
+ def turn_off_paperclip_logging
196
+ # Is there I way to silence the paperclip output? Yes...
197
+ Paperclip.options[:log] = false
198
+ # Is there I way to capture the paperclip output for comparison? Don't know.
199
+ end
200
+
201
+ end # ActiveSupportExtension::TestCase
202
+ ActiveSupport::TestCase.send(:include, CommonLib::ActiveSupportExtension::TestCase)
203
+ __END__
@@ -0,0 +1,48 @@
1
+ #
2
+ # copied from Paperclip
3
+ # I'm not real sure what most of this is for
4
+ # All I want is to share my rake tasks.
5
+ # I would be nice to share my locales as well
6
+ #
7
+ module CommonLib
8
+ require 'rails'
9
+
10
+ class Railtie < Rails::Railtie
11
+ # initializer 'paperclip.insert_into_active_record' do |app|
12
+ # ActiveSupport.on_load :active_record do
13
+ # Paperclip::Railtie.insert
14
+ # end
15
+ #
16
+ # if app.config.respond_to?(:paperclip_defaults)
17
+ # Paperclip::Attachment.default_options.merge!(app.config.paperclip_defaults)
18
+ # end
19
+ # end
20
+
21
+ rake_tasks {
22
+ #
23
+ # Use caution. This would load the files in the app's lib/tasks/
24
+ # Not sure if that was in addition to the gem, or instead of, but
25
+ # it loaded them twice so they ran twice.
26
+ #
27
+ # load "tasks/common_lib.rake"
28
+ # load "tasks/csv.rake"
29
+ # load "tasks/database.rake"
30
+
31
+
32
+ Dir["#{File.dirname(__FILE__)}/../tasks/**/*.rake"].sort.each { |ext| load ext }
33
+
34
+ }
35
+ end
36
+
37
+ # class Railtie
38
+ # def self.insert
39
+ # Paperclip.options[:logger] = Rails.logger
40
+ #
41
+ # if defined?(ActiveRecord)
42
+ # Paperclip.options[:logger] = ActiveRecord::Base.logger
43
+ # ActiveRecord::Base.send(:include, Paperclip::Glue)
44
+ # end
45
+ # end
46
+ # end
47
+ end
48
+
@@ -0,0 +1,8 @@
1
+ require "common_lib/ruby/array"
2
+ require "common_lib/ruby/fixnum"
3
+ require "common_lib/ruby/hash"
4
+ require "common_lib/ruby/integer"
5
+ require "common_lib/ruby/nil_class"
6
+ require "common_lib/ruby/numeric"
7
+ require "common_lib/ruby/object"
8
+ require "common_lib/ruby/string"
@@ -0,0 +1,128 @@
1
+ module CommonLib::Array# :nodoc:
2
+
3
+ # ['a','b','c'].arrange([2,0,1]) => ['c','a','b']
4
+ def arrange(new_array_index=[])
5
+ new_array = self.dup
6
+ new_array_index.each_with_index do |index,new_index|
7
+ new_array[new_index] = self[index]
8
+ end
9
+ new_array
10
+ end
11
+
12
+ # Remove all "blank?" items for the array
13
+ def drop_blanks!
14
+ delete_if{|a|a.blank?}
15
+ end
16
+
17
+ # Return capitlized versions of each item in the array
18
+ def capitalize
19
+ collect(&:capitalize)
20
+ end
21
+
22
+ # Capitalize each item in the array and return it
23
+ def capitalize!
24
+ each_with_index do |element,index|
25
+ self[index] = element.capitalize
26
+ end
27
+ self
28
+ end
29
+
30
+ # Return downcased versions of each item in the array
31
+ def downcase
32
+ collect(&:downcase)
33
+ end
34
+
35
+ # Return the average digitized value of the array
36
+ def average
37
+ if self.length > 0
38
+ # sum defined in activesupport/lib/active_support/core_ext/enumerable.rb
39
+ self.digitize.sum.to_f / self.length
40
+ else
41
+ nil
42
+ end
43
+ end
44
+
45
+ # Return the median digitized value of the array
46
+ def median
47
+ if self.length > 0
48
+ sorted_values = self.digitize.sort
49
+ length = sorted_values.length
50
+ if length.odd?
51
+ sorted_values[length/2]
52
+ else
53
+ ( sorted_values[length/2] + sorted_values[-1+length/2] ).to_f / 2
54
+ end
55
+ else
56
+ nil
57
+ end
58
+ end
59
+
60
+ # Return a copy of the array with values at the
61
+ # given indexes swapped.
62
+ def swap_indexes(i,j)
63
+ new_array = self.dup
64
+ new_array[i],new_array[j] = self[j],self[i]
65
+ new_array
66
+ end
67
+
68
+ # Swap the values of an array at the given indexes
69
+ # and return it
70
+ def swap_indexes!(i,j)
71
+ self[i],self[j] = self[j],self[i]
72
+ self
73
+ end
74
+
75
+ # Convert all items in the array to_f
76
+ def numericize
77
+ collect(&:to_f)
78
+ end
79
+ alias_method :digitize, :numericize
80
+
81
+ # def first_index(value = nil) # either way works
82
+
83
+ # return the first index of the array
84
+ # with a value matching that given
85
+ def first_index(value = nil, &block)
86
+ using_block = block_given?
87
+ each_with_index do |element,index|
88
+ return index if (using_block && yield(element)) || (value == element)
89
+ end
90
+ return nil
91
+ end
92
+
93
+ def to_boolean
94
+ !empty? && all?{|v| v.to_boolean }
95
+ end
96
+ # alias_method :true?, :to_boolean
97
+ alias_method :to_b, :to_boolean
98
+
99
+ # [].true?
100
+ # => false
101
+ # [true].true?
102
+ # => true
103
+ # [true,false].true?
104
+ # => true
105
+ # [false].true?
106
+ # => false
107
+ def true?
108
+ !empty? && any?{|v| v.true? }
109
+ end
110
+
111
+ def false?
112
+ !empty? && any?{|v| v.false? }
113
+ end
114
+
115
+ # I need to work on this one ...
116
+ def true_xor_false?
117
+ # self.include?('true') ^ self.include?('false') ^
118
+ # self.include?(true) ^ self.include?(false)
119
+ contains_true = contains_false = false
120
+ each {|v|
121
+ # ( v.to_boolean ) ? contains_true = true : contains_false = true
122
+ eval("contains_#{v.to_boolean}=true")
123
+ }
124
+ contains_true ^ contains_false
125
+ end
126
+
127
+ end
128
+ Array.send(:include, CommonLib::Array )