kilt-cms 0.9.13 → 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 (118) hide show
  1. checksums.yaml +4 -4
  2. data/README.rdoc +3 -5
  3. data/app/controllers/kilt/kilt_controller.rb +1 -1
  4. data/app/views/kilt/form/{boolean.html.erb → _boolean.html.erb} +0 -0
  5. data/app/views/kilt/form/{datetime.html.erb → _datetime.html.erb} +0 -0
  6. data/app/views/kilt/form/{file.html.erb → _file.html.erb} +1 -1
  7. data/app/views/kilt/form/{html.html.erb → _html.html.erb} +0 -0
  8. data/app/views/kilt/form/{image.html.erb → _image.html.erb} +1 -1
  9. data/app/views/kilt/form/{longtext.html.erb → _longtext.html.erb} +0 -0
  10. data/app/views/kilt/form/{number.html.erb → _number.html.erb} +0 -0
  11. data/app/views/kilt/form/{select_one.html.erb → _select_one.html.erb} +0 -0
  12. data/app/views/kilt/form/{text.html.erb → _text.html.erb} +0 -0
  13. data/app/views/kilt/form/{vimeo.html.erb → _vimeo.html.erb} +0 -0
  14. data/app/views/kilt/form/{youtube.html.erb → _youtube.html.erb} +0 -0
  15. data/app/views/kilt/kilt/_form.html.erb +12 -10
  16. data/app/views/kilt/kilt/edit.html.erb +1 -1
  17. data/app/views/kilt/kilt/index.html.erb +3 -2
  18. data/app/views/kilt/kilt/list.html.erb +6 -9
  19. data/app/views/kilt/kilt/new.html.erb +1 -1
  20. data/app/views/layouts/kilt/cms/_menu.html.erb +2 -6
  21. data/lib/generators/kilt/backend_generator.rb +28 -6
  22. data/lib/generators/kilt/templates/backend/create_kilt_objects.rb +13 -0
  23. data/lib/generators/kilt/templates/backend/creds.yml.example +10 -5
  24. data/lib/generators/kilt/templates/backend/creds.yml.rethinkdb.example +22 -0
  25. data/lib/kilt.rb +34 -54
  26. data/lib/kilt/base.rb +3 -2
  27. data/lib/kilt/db/active_record.rb +50 -0
  28. data/lib/kilt/db/rethink_db.rb +108 -0
  29. data/lib/kilt/form.rb +7 -5
  30. data/lib/kilt/formatting.rb +39 -0
  31. data/lib/kilt/object.rb +32 -31
  32. data/lib/kilt/utils.rb +32 -39
  33. data/lib/kilt_object.rb +3 -0
  34. data/test/dummy/config/application.rb +1 -1
  35. data/test/dummy/config/database.yml +5 -0
  36. data/test/dummy/config/initializers/kilt.rb +1 -1
  37. data/test/dummy/config/kilt/config.yml +4 -13
  38. data/test/dummy/config/kilt/creds.yml +16 -11
  39. data/test/dummy/config/kilt/creds.yml.rethinkdb.example +22 -0
  40. data/test/dummy/config/routes.rb +1 -1
  41. data/test/dummy/db/migrate/20140519110428_create_kilt_objects.rb +13 -0
  42. data/test/dummy/db/schema.rb +29 -0
  43. data/test/dummy/log/development.log +551 -0
  44. data/test/dummy/public/uploads/image/timbo.jpg +0 -0
  45. data/test/dummy/tmp/cache/assets/development/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  46. data/test/dummy/tmp/cache/assets/development/sprockets/18837248cb3274df9b7c6b0a5dcd56a5 +0 -0
  47. data/test/dummy/tmp/cache/assets/development/sprockets/20dffc9385ea740ceca470ee4e4b2197 +0 -0
  48. data/test/dummy/tmp/cache/assets/development/sprockets/23733156ea27da67211ba7ff763d76f0 +0 -0
  49. data/test/dummy/tmp/cache/assets/development/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  50. data/test/dummy/tmp/cache/assets/development/sprockets/3148655edeef1c319d57d0f9570acfd5 +0 -0
  51. data/test/dummy/tmp/cache/assets/development/sprockets/32ab48c63ad3bb6ddc1f419e51adb5db +0 -0
  52. data/test/dummy/tmp/cache/assets/development/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  53. data/test/dummy/tmp/cache/assets/development/sprockets/36715e644093cfcb33ac795f3c9d66fe +0 -0
  54. data/test/dummy/tmp/cache/assets/development/sprockets/36f9a7f6f43adb582ac6eec43b58eb30 +0 -0
  55. data/test/dummy/tmp/cache/assets/development/sprockets/3d8ca1585ee95c5efb388645d66cf377 +0 -0
  56. data/test/dummy/tmp/cache/assets/development/sprockets/413be9c73d25c7febe933f4619da7cf8 +0 -0
  57. data/test/dummy/tmp/cache/assets/development/sprockets/4a935342c0113b534ad7622234965544 +0 -0
  58. data/test/dummy/tmp/cache/assets/development/sprockets/4b5c4a1edd3823a6a6c39e06b6eb230a +0 -0
  59. data/test/dummy/tmp/cache/assets/development/sprockets/4c5736bf0bd429296d1a32930364897e +0 -0
  60. data/test/dummy/tmp/cache/assets/development/sprockets/4f26158b7a804a3e3c47ce0ceff2798c +0 -0
  61. data/test/dummy/tmp/cache/assets/development/sprockets/542fc810f9cd36e7bed9a3e5e9670995 +0 -0
  62. data/test/dummy/tmp/cache/assets/development/sprockets/56f1e7d57d31bf3315e1e33732d57faa +0 -0
  63. data/test/dummy/tmp/cache/assets/development/sprockets/5bea4c2d87ec725262213ed51575b485 +0 -0
  64. data/test/dummy/tmp/cache/assets/development/sprockets/6b6e86e81f5e8f80bb2b3d1e8f7ed7f3 +0 -0
  65. data/test/dummy/tmp/cache/assets/development/sprockets/6c2ddc22c04321c269de383e37cb519b +0 -0
  66. data/test/dummy/tmp/cache/assets/development/sprockets/7035e5c60f5da3b7fc12c472677483d0 +0 -0
  67. data/test/dummy/tmp/cache/assets/development/sprockets/704c682957c2beb38a1927ddb66cc557 +0 -0
  68. data/test/dummy/tmp/cache/assets/development/sprockets/74b6c3e00222b8bf31b0ecf50fcb0d38 +0 -0
  69. data/test/dummy/tmp/cache/assets/development/sprockets/91021bdb7ebb3d4f6fdf4e49909d4c07 +0 -0
  70. data/test/dummy/tmp/cache/assets/development/sprockets/914095efd3b33401a7f29f9fb05d72d6 +0 -0
  71. data/test/dummy/tmp/cache/assets/development/sprockets/9dd98bca318fb820a8451cafce020588 +0 -0
  72. data/test/dummy/tmp/cache/assets/development/sprockets/9f2c00a36b5b8c713a797b508c778a65 +0 -0
  73. data/test/dummy/tmp/cache/assets/development/sprockets/a88ab4e49bb7203b8e0586c38efdd2b0 +0 -0
  74. data/test/dummy/tmp/cache/assets/development/sprockets/acfc15d25fde8b933b06c91324755751 +0 -0
  75. data/test/dummy/tmp/cache/assets/development/sprockets/c0671678b70011547b80da0669d031cc +0 -0
  76. data/test/dummy/tmp/cache/assets/development/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  77. data/test/dummy/tmp/cache/assets/development/sprockets/d6973c1d43f8ca41b98a6b640fda2616 +0 -0
  78. data/test/dummy/tmp/cache/assets/development/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  79. data/test/dummy/tmp/cache/assets/development/sprockets/dc676330961fd22be77c48cb77ec065f +0 -0
  80. data/test/dummy/tmp/cache/assets/development/sprockets/ddc29f2c13cd99d017fd526b61dcb951 +0 -0
  81. data/test/dummy/tmp/cache/assets/development/sprockets/de45247cb814cafe20e9e46c79e470fe +0 -0
  82. data/test/dummy/tmp/cache/assets/development/sprockets/df0b7c3ac590a286561806e813c2974f +0 -0
  83. data/test/dummy/tmp/cache/assets/development/sprockets/dfa8b85e3c1273b7e69839e4e607c3e1 +0 -0
  84. data/test/dummy/tmp/cache/assets/development/sprockets/e22d705d0c889ec4837f183a9beec0f3 +0 -0
  85. data/test/dummy/tmp/cache/assets/development/sprockets/e3a4ad3549e1e57d03698a36daeff406 +0 -0
  86. data/test/dummy/tmp/cache/assets/development/sprockets/ef7c8ce46dfc9e9087ce84dbdfdd0011 +0 -0
  87. data/test/dummy/tmp/cache/assets/development/sprockets/f4b5dd06a6113950c0f481cbc5a31a66 +0 -0
  88. data/test/dummy/tmp/cache/assets/development/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  89. data/test/dummy/tmp/data/meta_request/1c3d4a4b-d7e1-4126-8bfc-572acbc2f675.json +1 -0
  90. data/test/dummy/tmp/data/meta_request/449ff78e-30d2-4cc5-bbb7-3397d04a4c46.json +1 -0
  91. data/test/dummy/tmp/data/meta_request/5974cfb9-99bd-4d5c-a4e1-6e352ecd2dd8.json +1 -0
  92. data/test/dummy/tmp/data/meta_request/960de12b-b1c5-4d44-937b-e3d8c00d682c.json +1 -0
  93. data/test/dummy/tmp/data/meta_request/9df9ad77-1b70-4fb7-ad18-f31f28c60c6c.json +1 -0
  94. data/test/dummy/tmp/data/meta_request/a5374758-4e16-487a-ab11-9a69a1abed2e.json +1 -0
  95. data/test/dummy/tmp/data/meta_request/a65d97b2-df1f-445e-99d5-615c4db5c4e1.json +1 -0
  96. data/test/dummy/tmp/data/meta_request/b3693c6f-23cd-4afe-ae82-38078eaa643a.json +1 -0
  97. data/test/dummy/tmp/data/meta_request/daa1c80a-bad3-4bc1-8f67-d586979cbedc.json +1 -0
  98. data/test/dummy/tmp/data/meta_request/e2e7621f-08e5-46dd-83c2-b87eb6d5fd45.json +1 -0
  99. data/test/empty.sqlite3 +0 -0
  100. data/test/generators/kilt/backend_generator_spec.rb +70 -0
  101. data/test/kilt/form_spec.rb +40 -51
  102. data/test/kilt/formatting_spec.rb +66 -0
  103. data/test/kilt/object_spec.rb +204 -0
  104. data/test/kilt/utils_spec.rb +147 -0
  105. data/test/kilt_object_spec.rb +12 -0
  106. data/test/kilt_spec.rb +422 -0
  107. data/test/minitest_helper.rb +58 -0
  108. metadata +164 -66
  109. data/test/dummy/tmp/data/meta_request/191f094a-5e87-4654-bc80-5acb51d3f903.json +0 -1
  110. data/test/dummy/tmp/data/meta_request/1eb9499d-2e84-47df-ac0a-da308031aa3a.json +0 -1
  111. data/test/dummy/tmp/data/meta_request/300cabd2-6e51-4b1b-ab55-a072049c2a0f.json +0 -1
  112. data/test/dummy/tmp/data/meta_request/3ec7ea74-e7a7-4f4d-9434-e08edfc12b8a.json +0 -1
  113. data/test/dummy/tmp/data/meta_request/5bb81411-321e-4cba-9ffa-87fcd8105ef4.json +0 -1
  114. data/test/dummy/tmp/data/meta_request/9d6e10c1-5b14-4199-852e-9f1e97132b8f.json +0 -1
  115. data/test/dummy/tmp/data/meta_request/a6403ed9-21f1-4a00-9725-4a1c544214f6.json +0 -1
  116. data/test/dummy/tmp/data/meta_request/b4487e0b-0a08-40a3-a364-e715e6765631.json +0 -1
  117. data/test/dummy/tmp/data/meta_request/d399f913-d3a5-4de3-b012-848377a702fc.json +0 -1
  118. data/test/dummy/tmp/data/meta_request/dcf95267-1855-4062-85c8-e880f8d62443.json +0 -1
@@ -0,0 +1,147 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../minitest_helper')
2
+
3
+ describe Kilt::Utils do
4
+
5
+ describe "setting up the database" do
6
+ it "should call setup on the current database" do
7
+
8
+ database = Object.new
9
+ Kilt::Utils.stubs(:database).returns database
10
+
11
+ database.expects(:setup!)
12
+
13
+ Kilt::Utils.setup_db
14
+
15
+ end
16
+ end
17
+
18
+ describe "database" do
19
+
20
+ ['test', 'development', 'production'].each do |environment|
21
+
22
+ describe "when current environment is #{environment}" do
23
+
24
+ before do
25
+ Kilt::Utils.instance_eval { @database = nil; @db_type = nil }
26
+ ENV.stubs(:[]).with('RAILS_ENV').returns environment
27
+ end
28
+
29
+ after { Kilt::Utils.instance_eval { @database = nil; @db_type = nil } }
30
+
31
+ it "should return a new Kilt database" do
32
+
33
+ database = Object.new
34
+
35
+ Kilt.stubs(:config).returns Object.new
36
+ Kilt.config.stubs(:[]).with(environment.to_sym).returns Object.new
37
+ Kilt.config[environment.to_sym].stubs(:[]).with(:db).returns Object.new
38
+
39
+ Kilt::DB::RethinkDb.stubs(:new)
40
+ .with(Kilt.config[environment.to_sym][:db])
41
+ .returns database
42
+
43
+ Kilt::Utils.database.must_be_same_as database
44
+
45
+ end
46
+
47
+ it "should return the same Kilt database everytime it is called" do
48
+
49
+ database = Object.new
50
+
51
+ Kilt.stubs(:config).returns Object.new
52
+ Kilt.config.stubs(:[]).with(environment.to_sym).returns Object.new
53
+ Kilt.config[environment.to_sym].stubs(:[]).with(:db).returns Object.new
54
+
55
+ Kilt::DB::RethinkDb.expects(:new)
56
+ .with(Kilt.config[environment.to_sym][:db])
57
+ .returns database
58
+
59
+ Kilt::Utils.database.must_be_same_as Kilt::Utils.database
60
+
61
+ end
62
+
63
+ describe "when config is done the old Kilt way, with a single 'db'" do
64
+
65
+ it "should return a new Kilt database with the development config" do
66
+
67
+ database = Object.new
68
+
69
+ Kilt.stubs(:config).returns Object.new
70
+ Kilt.config.stubs(:[]).returns nil
71
+ Kilt.config.stubs(:[]).with(:db).returns Object.new
72
+
73
+ Kilt::DB::RethinkDb.stubs(:new)
74
+ .with(Kilt.config[:db])
75
+ .returns database
76
+
77
+ Kilt::Utils.database.must_be_same_as database
78
+
79
+ end
80
+
81
+ end
82
+
83
+ end
84
+
85
+ end
86
+
87
+ ['', nil].each do |environment|
88
+
89
+ describe "when current environment is #{environment}" do
90
+
91
+ before do
92
+ Kilt::Utils.instance_eval { @database = nil; @db_type = nil }
93
+ ENV.stubs(:[]).with('RAILS_ENV').returns environment
94
+ end
95
+
96
+ after { Kilt::Utils.instance_eval { @database = nil; @db_type = nil } }
97
+
98
+ it "should return a new Kilt database with the development config" do
99
+
100
+ database = Object.new
101
+
102
+ Kilt.stubs(:config).returns Object.new
103
+ Kilt.config.stubs(:[]).with(:development).returns Object.new
104
+ Kilt.config[:development].stubs(:[]).with(:db).returns Object.new
105
+
106
+ Kilt::DB::RethinkDb.stubs(:new)
107
+ .with(Kilt.config[:development][:db])
108
+ .returns database
109
+
110
+ Kilt::Utils.database.must_be_same_as database
111
+
112
+ end
113
+
114
+ end
115
+
116
+ end
117
+
118
+ end
119
+
120
+ describe "slugify a string" do
121
+ it "should replace spaces with dashes" do
122
+ Kilt::Utils.slugify("this works").must_equal "this-works"
123
+ end
124
+ it "should remove apostrophes" do
125
+ Kilt::Utils.slugify("this's working").must_equal "thiss-working"
126
+ end
127
+ it "should replace @ and & with at and and" do
128
+ Kilt::Utils.slugify("hands @ two & ten").must_equal "hands-at-two-and-ten"
129
+ end
130
+ it "should replace all non alphanumeric characters with dash" do
131
+ Kilt::Utils.slugify("a!b@c#d$e%f^g&h*i(j)k_l.m,n<o>p[q]r/s|t?u;v~w{x}y+z").must_equal "a-b-at-c-d-e-f-g-and-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z"
132
+ end
133
+ it "should replace periods and underscores with dashes" do
134
+ Kilt::Utils.slugify("this_works.now").must_equal "this-works-now"
135
+ end
136
+ it "should convert double dash to single" do
137
+ Kilt::Utils.slugify("this--works").must_equal "this-works"
138
+ end
139
+ it "should strip leading and trailing dashes" do
140
+ Kilt::Utils.slugify("-this-works-").must_equal "this-works"
141
+ end
142
+ it "should downcase the string" do
143
+ Kilt::Utils.slugify("THIS WORKS").must_equal "this-works"
144
+ end
145
+ end
146
+
147
+ end
@@ -0,0 +1,12 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/minitest_helper')
2
+
3
+ describe KiltObject do
4
+
5
+ before do
6
+ clear_out_the_database
7
+ end
8
+
9
+ it "should exist" do
10
+ KiltObject.delete_all
11
+ end
12
+ end
data/test/kilt_spec.rb ADDED
@@ -0,0 +1,422 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/minitest_helper')
2
+
3
+ describe Kilt do
4
+
5
+ before do
6
+ clear_out_the_database
7
+ end
8
+
9
+ describe "types" do
10
+
11
+ it "should return the keys from the objects in the Kilt config" do
12
+ keys = default_test_config.objects.map { |k, _| k }
13
+ Kilt.types.must_equal keys
14
+ end
15
+
16
+ end
17
+
18
+ persistence_models_to_test = [['using active record for persistence', :active_record]]
19
+ unless ENV['TRAVIS']
20
+ persistence_models_to_test << ['using rethinkdb for persistence', :rethinkdb]
21
+ end
22
+
23
+ persistence_models_to_test.map { |args| Struct.new(:description, :db_type).new(*args) }.each do |persistence|
24
+
25
+ describe persistence.description do
26
+
27
+ before do
28
+ Kilt::Utils.use_db persistence.db_type
29
+ end
30
+
31
+ describe "creating an object" do
32
+
33
+ [
34
+ ['1/1/2014', 'cat', 'Apple', Time.parse('1/1/2014').to_i.to_s + "000", 'apple' ],
35
+ ['2/2/2016', 'dog', 'Orange', Time.parse('2/2/2016').to_i.to_s + "000", 'orange'],
36
+ ].map { |v| Struct.new(:today, :type, :name, :unique_id, :slug).new *v }.each do |scenario|
37
+
38
+ describe "basic scenarios" do
39
+
40
+ let(:values) { {} }
41
+
42
+ let(:object) do
43
+ Kilt::Object.new(scenario.type, values)
44
+ end
45
+
46
+ before do
47
+ Timecop.freeze Time.parse(scenario.today)
48
+ values['name'] = scenario.name
49
+ end
50
+
51
+ it "should set a unique id based on time" do
52
+ Kilt.create(object)
53
+ object['unique_id'].must_equal scenario.unique_id
54
+ end
55
+
56
+ it "should set the name" do
57
+ Kilt.create(object)
58
+ object['name'].must_equal scenario.name
59
+ end
60
+
61
+ it "should create an object that can be retrieved out of the database again" do
62
+ Kilt.create(object)
63
+ fresh = Kilt.get(object['slug'])
64
+ fresh['unique_id'].must_equal object['unique_id']
65
+ end
66
+
67
+ it "should set the type of the object" do
68
+ Kilt.create(object)
69
+ object['type'].must_equal scenario.type
70
+ Kilt.get(object['slug'])['type'].must_equal scenario.type
71
+ end
72
+
73
+ it "should set the created_at" do
74
+ Kilt.create(object)
75
+ object['created_at'].must_equal Time.parse(scenario.today)
76
+ Kilt.get(object['slug'])['created_at'].must_equal Time.parse(scenario.today)
77
+ end
78
+
79
+ it "should set the updated_at" do
80
+ Kilt.create(object)
81
+ object['updated_at'].must_equal Time.parse(scenario.today)
82
+ Kilt.get(object['slug'])['updated_at'].must_equal Time.parse(scenario.today)
83
+ end
84
+
85
+ it "should set a slug on the object" do
86
+ Kilt.create(object)
87
+ object['slug'].must_equal scenario.slug
88
+ end
89
+
90
+ if persistence.db_type == :active_record
91
+ it "should set the unique_id for the object" do
92
+ Kilt.create(object)
93
+ KiltObject.first.unique_id.must_equal object['unique_id']
94
+ end
95
+ end
96
+
97
+ describe "creating the record twice" do
98
+
99
+ it "should keep the slugs unique by appending the id to the second record" do
100
+
101
+ first = Kilt::Object.new(scenario.type, values)
102
+ second = Kilt::Object.new(scenario.type, values.clone)
103
+
104
+ Kilt.create(first)
105
+ Timecop.freeze(Time.now + 5)
106
+ Kilt.create(second)
107
+
108
+ first['slug'].must_equal scenario.slug
109
+ second['slug'].must_equal "#{scenario.slug}-#{second['unique_id']}"
110
+
111
+ end
112
+
113
+ end
114
+
115
+ end
116
+
117
+ end
118
+
119
+ [1, 2, 3, 4].each do |error_count|
120
+
121
+ describe "when creating a record fails" do
122
+ if persistence.db_type == :active_record
123
+ it "should return false" do
124
+ KiltObject.stubs(:create!).raises 'error'
125
+ object = Kilt::Object.new('dog', { 'name' => 'A name.' } )
126
+ Kilt.create(object).must_equal false
127
+ end
128
+ else
129
+ it "should return false" do
130
+ Kilt::DB::RethinkDb.any_instance.stubs(:execute).returns( { 'errors' => error_count } )
131
+ object = Kilt::Object.new('dog', { 'name' => 'A name.' } )
132
+ Kilt.create(object).must_equal false
133
+ end
134
+ end
135
+ end
136
+
137
+ end
138
+
139
+ end
140
+
141
+ describe "updating an object" do
142
+
143
+ [
144
+ ['1/1/2012', '2/3/2012', 'cat', 'test', 'something_else', 'A', 'B', 'test'],
145
+ ['2/3/2014', '5/3/2015', 'dog', 'apple', 'orange', 'L', 'S', 'apple'],
146
+ ].map do |values|
147
+ Struct.new(:created_at, :updated_at, :type, :original_name, :new_name, :original_size, :new_size, :original_slug).new *values
148
+ end.each do |scenario|
149
+
150
+ describe "basic scenarios" do
151
+
152
+ let(:values) { {
153
+ 'name' => scenario.original_name,
154
+ 'size' => scenario.original_size
155
+ } }
156
+
157
+ let(:object) do
158
+ Kilt::Object.new(scenario.type, values)
159
+ end
160
+
161
+ before do
162
+ Kilt.create(object)
163
+ Timecop.freeze Time.parse(scenario.created_at)
164
+ values['name'] = scenario.new_name
165
+ values['size'] = scenario.new_size
166
+ end
167
+
168
+ it "should use the updated at date" do
169
+ new_date = Time.parse(scenario.updated_at)
170
+
171
+ Timecop.freeze new_date
172
+ Kilt.update object.slug, object
173
+ object['updated_at'].must_equal new_date
174
+ Kilt.get(object['slug'])['updated_at'].must_equal new_date
175
+ end
176
+
177
+ it "should update the name" do
178
+ Kilt.update object.slug, object
179
+
180
+ object['name'].must_equal scenario.new_name
181
+ Kilt.get(object['slug'])['name'].must_equal scenario.new_name
182
+ end
183
+
184
+ it "should update the size" do
185
+ Kilt.update object.slug, object
186
+
187
+ object['size'].must_equal scenario.new_size
188
+ Kilt.get(object['slug'])['size'].must_equal scenario.new_size
189
+ end
190
+
191
+ it "should NOT update the slug" do
192
+ Kilt.update object.slug, object
193
+
194
+ object['slug'].must_equal scenario.original_slug
195
+ end
196
+
197
+ end
198
+
199
+ end
200
+
201
+ describe "updating the slug" do
202
+
203
+ describe "when the slug is unique" do
204
+
205
+ it "should update the slug" do
206
+
207
+ object = Kilt::Object.new('cat', { 'name' => 'test' } )
208
+ Kilt.create object
209
+
210
+ original_slug = object['slug']
211
+
212
+ object['slug'] = 'different'
213
+ Kilt.update original_slug, object
214
+
215
+ Kilt.get(object['slug'])['unique_id'].must_equal object['unique_id']
216
+ end
217
+
218
+ end
219
+
220
+ describe "when the slug is not unique" do
221
+
222
+ before do
223
+ Timecop.freeze Time.parse('1/1/2014')
224
+ end
225
+
226
+ it "should change the slug to a unique value" do
227
+
228
+ first = Kilt::Object.new('cat', { 'name' => 'first' } )
229
+ Kilt.create first
230
+
231
+ # have to update the time, as the unique id is time-based
232
+ Timecop.freeze Time.now + 1
233
+
234
+ second = Kilt::Object.new('cat', { 'name' => 'second' } )
235
+ Kilt.create second
236
+
237
+ slug = first['slug']
238
+
239
+ first['slug'] = second['slug']
240
+ Kilt.update 'first', first
241
+
242
+ first['slug'].must_equal 'second-' + Time.now.to_i.to_s + "000"
243
+ end
244
+
245
+ end
246
+
247
+ [
248
+ ['do_not_clear_me', nil],
249
+ ['do_not_clear_me', ''],
250
+ ['do_not_clear_me', ' '],
251
+ ].map { |a| Struct.new(:name, :invalid_value).new *a }.each do |scenario|
252
+
253
+ describe "when the slug is removed" do
254
+
255
+ before do
256
+ Timecop.freeze Time.parse('1/1/2014')
257
+ end
258
+
259
+ it "should keep a valid slug" do
260
+
261
+ clear_out_the_database
262
+
263
+ first = Kilt::Object.new('cat', { 'name' => scenario.name } )
264
+ Kilt.create first
265
+
266
+ slug = first['slug']
267
+
268
+ first['slug'] = scenario.invalid_value
269
+ Kilt.update slug, first
270
+
271
+ first['slug'].must_equal slug
272
+ end
273
+
274
+ end
275
+
276
+ describe "when the slug is removed and the slug will collide with another" do
277
+
278
+ before do
279
+ Timecop.freeze Time.parse('1/1/2014')
280
+ clear_out_the_database
281
+ end
282
+
283
+ it "should keep a valid slug" do
284
+
285
+ clear_out_the_database
286
+
287
+ first = Kilt::Object.new('dog', { 'name' => 'a unique value' } )
288
+ Kilt.create first
289
+
290
+ Timecop.freeze Time.now + 1
291
+
292
+ second = Kilt::Object.new('dog', { 'name' => scenario.name } )
293
+ Kilt.create second
294
+
295
+ slug = first['slug']
296
+
297
+ first['name'] = scenario.name
298
+ first['slug'] = scenario.invalid_value
299
+ Kilt.update slug, first
300
+
301
+ first['slug'].must_equal second['slug'] + '-' + Time.now.to_i.to_s + "000"
302
+ end
303
+
304
+ end
305
+
306
+ end
307
+
308
+ end
309
+
310
+ end
311
+
312
+ [
313
+ ['dog', 'rover'],
314
+ ['cat', 'fluffy'],
315
+ ['dog', 'x'],
316
+ ['cat', 'y']
317
+ ].map { |args| Struct.new(:type, :name).new(*args) }.each do |scenario|
318
+
319
+ describe "getting an object" do
320
+
321
+ it "should return the object" do
322
+ object = Kilt::Object.new(scenario.type, { 'name' => scenario.name } )
323
+ Kilt.create object
324
+
325
+ result = Kilt.get object['slug']
326
+
327
+ result['unique_id'].must_equal object['unique_id']
328
+ end
329
+
330
+ it "should return a kilt object" do
331
+ object = Kilt::Object.new(scenario.type, { 'name' => scenario.name } )
332
+ Kilt.create object
333
+
334
+ result = Kilt.get object['slug']
335
+
336
+ result.is_a? Kilt::Object
337
+ end
338
+
339
+ describe "no match" do
340
+ it "should return nil" do
341
+ Kilt.get(scenario.name).nil?.must_equal true
342
+ end
343
+ end
344
+
345
+ end
346
+
347
+ end
348
+
349
+ describe "get collection" do
350
+
351
+ it "should return an object collection" do
352
+ Kilt.get_collection('cat').is_a?(Kilt::ObjectCollection).must_equal true
353
+ end
354
+
355
+ [
356
+ ['cat', 'cat'],
357
+ ['dog', 'dog'],
358
+ ['cats', 'cat'],
359
+ ['dogs', 'dog'],
360
+ ].map { |args| Struct.new(:type, :singular_type).new(*args) }.each do |scenario|
361
+
362
+ describe "objects exist" do
363
+ before do
364
+ Timecop.freeze Time.parse('3/4/2018')
365
+ ['dog', 'cat'].each do |type|
366
+ (1..4).map { |x| Kilt::Object.new(type, { 'name' => "#{type}#{x}" } ) }
367
+ .each do |object|
368
+ Timecop.freeze Time.now + 5
369
+ Kilt.create object
370
+ end
371
+ end
372
+ end
373
+
374
+ it "should return only the objects tied to this type" do
375
+ results = Kilt.get_collection scenario.type
376
+ results.count.must_equal 4
377
+ results.each { |x| x['type'].must_equal scenario.singular_type }
378
+ end
379
+
380
+ end
381
+
382
+ end
383
+
384
+ end
385
+
386
+ describe "delete" do
387
+
388
+ let(:object) do
389
+ o = Kilt::Object.new('cat', { 'name' => 'Anthem' })
390
+ Kilt.create o
391
+ o
392
+ end
393
+
394
+ it "should remove the record from the database" do
395
+ Kilt.delete object['slug']
396
+
397
+ Kilt.get(object['slug']).nil?.must_equal true
398
+ end
399
+
400
+ it "should return true if the delete did not error" do
401
+ Kilt.delete(object['slug']).must_equal true
402
+ end
403
+
404
+ if persistence.db_type == :active_record
405
+ it "should return false if the delete returned errors" do
406
+ KiltObject.any_instance.stubs(:delete).raises 'error'
407
+ Kilt.delete(object['slug']).must_equal false
408
+ end
409
+ else
410
+ it "should return false if the delete returned errors" do
411
+ Kilt::DB::RethinkDb.any_instance.stubs(:execute).returns( { 'errors' => 1 } )
412
+ Kilt.delete(object['slug']).must_equal false
413
+ end
414
+ end
415
+
416
+ end
417
+
418
+ end
419
+
420
+ end
421
+
422
+ end