librails 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3cabed066ea2a94382c29ce59130ad6dfb4204c2
4
- data.tar.gz: 8560c3972008e3824447c103ac92cf2387496b85
3
+ metadata.gz: 7277bb7e704d7bafe62739abd7a89d944672615b
4
+ data.tar.gz: 832563e9d0fb0eb8b13bb2d41b6965da843e3a3c
5
5
  SHA512:
6
- metadata.gz: 971260575f0d93f86fa98027602871ebea95a684d59dfc07aa6533dc51e6f8b3892a63c6d75bd7be88bfc97ba2004446ead5445dd0ba42ac429e13f935e2de05
7
- data.tar.gz: a70c8f84a26ccb50052b2d477df805931ab48cd85be61393d07413b1511a2bbf2bc4580380e480411139ded8ed7109586b5e310c69c3a77095ae03021a770583
6
+ metadata.gz: d22684b43a390bca7a8b40f32fec9d7198437acd14a4e2aca4be4d8dba25ea23f857583f80f73486886daa342d35880c95eaf7ae86e6521b26d0d477bb3ed7df
7
+ data.tar.gz: b9d3b554f73ba12e467cbeadb365d5532ba6cb4786505575a12c931fe4a7112fe4cc155a747fa2d7162c2e2750164263b585f7cc465f36341102d03ef6b4facb
data/lib/librails.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "librails/engine"
2
2
  #require "librails/version"
3
3
  require "librails/misc_utils"
4
+ require "librails/model_utils"
4
5
 
5
6
  module Librails
6
7
  end
@@ -0,0 +1,108 @@
1
+ module Librails
2
+ # Model related utilities
3
+ class SortItem
4
+ attr_accessor :item
5
+ attr_accessor :id, :array_index
6
+ end
7
+
8
+ class ModelUtils
9
+ def self.set_array_index(item, attr_name, index)
10
+ set_disp_order(item, attr_name, index + 1)
11
+ end
12
+
13
+ def self.get_array_index(item, attr_name)
14
+ get_disp_order(item, attr_name) - 1
15
+ end
16
+
17
+ def self.set_disp_order(item, attr_name, value)
18
+ item.send("#{attr_name}=", value)
19
+ end
20
+
21
+ def self.get_disp_order(item, attr_name)
22
+ item.send(attr_name)
23
+ end
24
+
25
+ def self.reorder(items, hash, attr_name = 'disp_order', hash_key = 'disp_order')
26
+ #items: ソート対象のオブジェクト(attr_nameで指定された属性で並び替えられている)
27
+ #hash: idと順番のハッシュ(hash_keyで指定された属性に新しい順番が指定されている)
28
+ # hash = {
29
+ # '10' => {'disp_order' => '1'},
30
+ # '20' => {'disp_order' => '2'},
31
+ # '30' => {'disp_order' => '2'},
32
+ # '40' => {'disp_order' => '1'},
33
+ #}
34
+
35
+ # 最初に1からインデックスを振り直す
36
+ items.each_with_index do |item, index|
37
+ set_array_index(item, attr_name, index)
38
+ end
39
+
40
+ # 前準備、ソート対象になるオブジェクトをまとめる
41
+ sort_items = []
42
+ hash.keys.each do |idstr|
43
+ disp_order = hash[idstr][hash_key]
44
+ next unless disp_order
45
+ new_index = disp_order.to_i - 1
46
+
47
+ # 範囲内に補正
48
+ if new_index < 0
49
+ new_index = 0
50
+ end
51
+ if new_index >= items.size - 1
52
+ new_index = items.size - 1
53
+ end
54
+
55
+ # IDでオブジェクトを検索
56
+ id = idstr.to_i
57
+ item = items.find{|s| s.id == id}
58
+ next if get_array_index(item, attr_name) == new_index #変化が無い場合は次へ
59
+
60
+ # index=順番に変更があるものはsorted_itemsに記憶
61
+ sort_item = SortItem.new
62
+ sort_item.item = item
63
+ sort_item.id = id
64
+ sort_item.array_index = new_index
65
+ sort_items << sort_item
66
+ end
67
+
68
+ # 入れ替える前にソートしておく。ただしい順で番号を入れ直すために必要。
69
+ # 例えば以下のような指定を考える
70
+ # 10
71
+ # 20
72
+ # 30
73
+ # 40 - 2
74
+ # 50 - 1
75
+ # a)2,1の順で入れると以下のようになる
76
+ # 50 - 1
77
+ # 10
78
+ # 40 - 2(3番目になってしまっている)
79
+ # 20
80
+ # 30
81
+ # b)1,2の順で入れると正しい
82
+ # 50 - 1
83
+ # 40 - 2
84
+ # 10
85
+ # 20
86
+ # 30
87
+
88
+ sort_items.sort!{|a, b|
89
+ r = a.array_index <=> b.array_index
90
+ if r == 0
91
+ r = a.id <=> b.id
92
+ end
93
+ r
94
+ }
95
+ # 新しい位置に挿入
96
+ sort_items.each do |sort_item|
97
+ item = sort_item.item
98
+ items.delete_at(items.index(item)) #ソートの途中で順番が変わっている可能性があるのでその都度検索する必要あり。deleteはdbが削除されるので使えない
99
+ items[sort_item.array_index, 0] = item
100
+ end
101
+ items.each_with_index do |item, index|
102
+ set_array_index(item, attr_name, index)
103
+ # item.child_index = index + 1
104
+ # print "after #{index} title=#{item.title} #{item.child_index}\n"
105
+ end
106
+ end
107
+ end
108
+ end
@@ -1,3 +1,3 @@
1
1
  module Librails
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.1"
3
3
  end
Binary file
@@ -88,3 +88,38 @@ Started GET "/assets/books.self-19a187bec6cdb96d6de80a61c16c857c613536adf9138476
88
88
 
89
89
 
90
90
  Started GET "/assets/application.self-8f06a73c35179188914ab50e057157639fce1401c1cdca640ac9cec33746fc5b.js?body=1" for ::1 at 2016-10-25 14:29:38 +0900
91
+
92
+
93
+ Started GET "/" for 127.0.0.1 at 2017-03-15 15:21:27 +0900
94
+ ActiveRecord::SchemaMigration Load (0.4ms) SELECT "schema_migrations".* FROM "schema_migrations"
95
+ Processing by Rails::WelcomeController#index as HTML
96
+ Rendered /Users/sora/work/librails/vendor/bundle/ruby/2.4.0/gems/railties-4.2.8/lib/rails/templates/rails/welcome/index.html.erb (1.8ms)
97
+ Completed 200 OK in 11ms (Views: 11.2ms | ActiveRecord: 0.0ms)
98
+
99
+
100
+ Started GET "/books" for 127.0.0.1 at 2017-03-15 15:21:34 +0900
101
+ Processing by BooksController#index as HTML
102
+ Book Load (0.5ms) SELECT "books".* FROM "books"
103
+ Rendered books/index.html.erb within layouts/application (11.5ms)
104
+ Completed 200 OK in 217ms (Views: 212.7ms | ActiveRecord: 0.6ms)
105
+
106
+
107
+ Started GET "/assets/books.self-b60acab5af8d245ed2b3012f77d11d8d207d8eada27756badaf4ef05770d60f6.css?body=1" for 127.0.0.1 at 2017-03-15 15:21:34 +0900
108
+
109
+
110
+ Started GET "/assets/scaffold.self-83b741db49389dc7cfdf85bf7537a0219cce48e085c4116afc83d55c9af47c78.css?body=1" for 127.0.0.1 at 2017-03-15 15:21:34 +0900
111
+
112
+
113
+ Started GET "/assets/books.self-19a187bec6cdb96d6de80a61c16c857c613536adf9138476bd367db38d282635.js?body=1" for 127.0.0.1 at 2017-03-15 15:21:34 +0900
114
+
115
+
116
+ Started GET "/assets/application.self-e80e8f2318043e8af94dddc2adad5a4f09739a8ebb323b3ab31cd71d45fd9113.css?body=1" for 127.0.0.1 at 2017-03-15 15:21:34 +0900
117
+
118
+
119
+ Started GET "/assets/application.self-8f06a73c35179188914ab50e057157639fce1401c1cdca640ac9cec33746fc5b.js?body=1" for 127.0.0.1 at 2017-03-15 15:21:34 +0900
120
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
121
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
122
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations"
123
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations"
124
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
125
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
@@ -648,3 +648,282 @@ Processing by BooksController#index as HTML
648
648
  Rendered books/index.html.erb within layouts/application (2.8ms)
649
649
  Completed 200 OK in 146ms (Views: 145.8ms | ActiveRecord: 0.1ms)
650
650
   (0.1ms) rollback transaction
651
+ ActiveRecord::SchemaMigration Load (0.6ms) SELECT "schema_migrations".* FROM "schema_migrations"
652
+  (0.1ms) begin transaction
653
+ Fixture Delete (0.8ms) DELETE FROM "books"
654
+ Fixture Insert (0.6ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 06:31:14', '2017-03-15 06:31:14', 980190962)
655
+ Fixture Insert (0.1ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 06:31:14', '2017-03-15 06:31:14', 298486374)
656
+  (1.0ms) commit transaction
657
+  (0.1ms) begin transaction
658
+ ---------------------------------------
659
+ MiscUtilsTest: test_rescue_mysql2_error
660
+ ---------------------------------------
661
+  (0.0ms) rollback transaction
662
+ ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
663
+  (0.2ms) begin transaction
664
+ Fixture Delete (0.4ms) DELETE FROM "books"
665
+ Fixture Insert (0.3ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 06:31:47', '2017-03-15 06:31:47', 980190962)
666
+ Fixture Insert (0.1ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 06:31:47', '2017-03-15 06:31:47', 298486374)
667
+  (8.4ms) commit transaction
668
+  (0.2ms) begin transaction
669
+ ---------------------------------------
670
+ MiscUtilsTest: test_rescue_mysql2_error
671
+ ---------------------------------------
672
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
673
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
674
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
675
+  (0.2ms) begin transaction
676
+ Fixture Delete (0.4ms) DELETE FROM "books"
677
+ Fixture Insert (0.2ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('One', 'MyString', 'MyText', '2017-03-15 07:10:10', '2017-03-15 07:10:10', 980190962)
678
+ Fixture Insert (0.1ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 07:10:10', '2017-03-15 07:10:10', 298486374)
679
+  (0.9ms) commit transaction
680
+  (0.1ms) begin transaction
681
+ -------------------
682
+ BookTest: test_book
683
+ -------------------
684
+ Book Load (0.4ms) SELECT "books".* FROM "books" WHERE "books"."id" = ? LIMIT 1 [["id", 980190962]]
685
+  (0.4ms) rollback transaction
686
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
687
+  (0.2ms) begin transaction
688
+ Fixture Delete (0.5ms) DELETE FROM "books"
689
+ Fixture Insert (0.3ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('One', 'MyString', 'MyText', '2017-03-15 07:10:37', '2017-03-15 07:10:37', 980190962)
690
+ Fixture Insert (0.1ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 07:10:37', '2017-03-15 07:10:37', 298486374)
691
+  (1.0ms) commit transaction
692
+  (0.1ms) begin transaction
693
+ -------------------
694
+ BookTest: test_book
695
+ -------------------
696
+ Book Load (0.4ms) SELECT "books".* FROM "books" WHERE "books"."id" = ? LIMIT 1 [["id", 980190962]]
697
+  (0.2ms) rollback transaction
698
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
699
+  (0.2ms) begin transaction
700
+ Fixture Delete (0.4ms) DELETE FROM "books"
701
+ Fixture Insert (0.1ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('One', 'MyString', 'MyText', '2017-03-15 09:15:29', '2017-03-15 09:15:29', 980190962)
702
+ Fixture Insert (0.1ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 09:15:29', '2017-03-15 09:15:29', 298486374)
703
+  (0.8ms) commit transaction
704
+  (0.0ms) begin transaction
705
+ ------------------------------------
706
+ ModelUtilsTest: test_reorder_forward
707
+ ------------------------------------
708
+  (0.1ms) rollback transaction
709
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
710
+  (0.1ms) begin transaction
711
+ Fixture Delete (0.5ms) DELETE FROM "books"
712
+ Fixture Insert (0.2ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('One', 'MyString', 'MyText', '2017-03-15 09:16:36', '2017-03-15 09:16:36', 980190962)
713
+ Fixture Insert (0.1ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 09:16:36', '2017-03-15 09:16:36', 298486374)
714
+  (8.3ms) commit transaction
715
+  (0.1ms) begin transaction
716
+ ------------------------------------
717
+ ModelUtilsTest: test_reorder_forward
718
+ ------------------------------------
719
+  (0.1ms) rollback transaction
720
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
721
+  (0.2ms) begin transaction
722
+ Fixture Delete (0.8ms) DELETE FROM "books"
723
+ Fixture Insert (0.2ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('One', 'MyString', 'MyText', '2017-03-15 09:17:12', '2017-03-15 09:17:12', 980190962)
724
+ Fixture Insert (0.1ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 09:17:12', '2017-03-15 09:17:12', 298486374)
725
+  (8.5ms) commit transaction
726
+  (0.1ms) begin transaction
727
+ ---------------------------------------
728
+ MiscUtilsTest: test_rescue_mysql2_error
729
+ ---------------------------------------
730
+  (0.0ms) rollback transaction
731
+  (0.6ms) rollback transaction
732
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
733
+  (0.1ms) begin transaction
734
+ Fixture Delete (0.5ms) DELETE FROM "books"
735
+ Fixture Insert (0.3ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('One', 'MyString', 'MyText', '2017-03-15 09:18:15', '2017-03-15 09:18:15', 980190962)
736
+ Fixture Insert (0.1ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 09:18:15', '2017-03-15 09:18:15', 298486374)
737
+  (8.3ms) commit transaction
738
+  (0.1ms) begin transaction
739
+ ------------------------------------
740
+ ModelUtilsTest: test_reorder_forward
741
+ ------------------------------------
742
+  (0.2ms) rollback transaction
743
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
744
+  (0.1ms) begin transaction
745
+ Fixture Delete (0.9ms) DELETE FROM "books"
746
+ Fixture Insert (0.2ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('One', 'MyString', 'MyText', '2017-03-15 09:18:59', '2017-03-15 09:18:59', 980190962)
747
+ Fixture Insert (0.1ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 09:18:59', '2017-03-15 09:18:59', 298486374)
748
+  (8.3ms) commit transaction
749
+  (0.1ms) begin transaction
750
+ ------------------------------------
751
+ ModelUtilsTest: test_reorder_forward
752
+ ------------------------------------
753
+  (0.0ms) rollback transaction
754
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
755
+  (0.1ms) begin transaction
756
+ Fixture Delete (0.5ms) DELETE FROM "books"
757
+ Fixture Insert (0.2ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('One', 'MyString', 'MyText', '2017-03-15 09:20:07', '2017-03-15 09:20:07', 980190962)
758
+ Fixture Insert (0.1ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 09:20:07', '2017-03-15 09:20:07', 298486374)
759
+  (8.4ms) commit transaction
760
+  (0.1ms) begin transaction
761
+ ------------------------------------
762
+ ModelUtilsTest: test_reorder_forward
763
+ ------------------------------------
764
+  (0.1ms) rollback transaction
765
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
766
+  (0.1ms) begin transaction
767
+ Fixture Delete (0.5ms) DELETE FROM "books"
768
+ Fixture Insert (0.2ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('One', 'MyString', 'MyText', '2017-03-15 09:21:32', '2017-03-15 09:21:32', 980190962)
769
+ Fixture Insert (0.1ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 09:21:32', '2017-03-15 09:21:32', 298486374)
770
+  (8.3ms) commit transaction
771
+  (0.1ms) begin transaction
772
+ ------------------------------------
773
+ ModelUtilsTest: test_reorder_forward
774
+ ------------------------------------
775
+  (0.1ms) rollback transaction
776
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
777
+  (0.2ms) begin transaction
778
+ Fixture Delete (0.6ms) DELETE FROM "books"
779
+ Fixture Insert (0.2ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('One', 'MyString', 'MyText', '2017-03-15 09:22:06', '2017-03-15 09:22:06', 980190962)
780
+ Fixture Insert (0.1ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 09:22:06', '2017-03-15 09:22:06', 298486374)
781
+  (0.8ms) commit transaction
782
+  (0.1ms) begin transaction
783
+ ------------------------------------
784
+ ModelUtilsTest: test_reorder_forward
785
+ ------------------------------------
786
+  (0.1ms) rollback transaction
787
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
788
+  (0.1ms) begin transaction
789
+ Fixture Delete (0.7ms) DELETE FROM "books"
790
+ Fixture Insert (0.2ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('One', 'MyString', 'MyText', '2017-03-15 09:22:42', '2017-03-15 09:22:42', 980190962)
791
+ Fixture Insert (0.1ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 09:22:42', '2017-03-15 09:22:42', 298486374)
792
+  (8.3ms) commit transaction
793
+  (0.1ms) begin transaction
794
+ ------------------------------------
795
+ ModelUtilsTest: test_reorder_forward
796
+ ------------------------------------
797
+  (0.1ms) rollback transaction
798
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
799
+  (0.1ms) begin transaction
800
+ Fixture Delete (0.3ms) DELETE FROM "books"
801
+ Fixture Insert (0.1ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('One', 'MyString', 'MyText', '2017-03-15 09:23:39', '2017-03-15 09:23:39', 980190962)
802
+ Fixture Insert (0.1ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 09:23:39', '2017-03-15 09:23:39', 298486374)
803
+  (8.4ms) commit transaction
804
+  (0.1ms) begin transaction
805
+ ------------------------------------
806
+ ModelUtilsTest: test_reorder_forward
807
+ ------------------------------------
808
+  (0.1ms) rollback transaction
809
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
810
+  (0.2ms) begin transaction
811
+ Fixture Delete (0.7ms) DELETE FROM "books"
812
+ Fixture Insert (0.2ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('One', 'MyString', 'MyText', '2017-03-15 09:24:22', '2017-03-15 09:24:22', 980190962)
813
+ Fixture Insert (0.1ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 09:24:22', '2017-03-15 09:24:22', 298486374)
814
+  (8.6ms) commit transaction
815
+  (0.1ms) begin transaction
816
+ ------------------------------------
817
+ ModelUtilsTest: test_reorder_forward
818
+ ------------------------------------
819
+  (0.0ms) rollback transaction
820
+ ActiveRecord::SchemaMigration Load (0.4ms) SELECT "schema_migrations".* FROM "schema_migrations"
821
+  (0.2ms) begin transaction
822
+ Fixture Delete (1.2ms) DELETE FROM "books"
823
+ Fixture Insert (0.6ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('One', 'MyString', 'MyText', '2017-03-15 10:08:42', '2017-03-15 10:08:42', 980190962)
824
+ Fixture Insert (0.1ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 10:08:42', '2017-03-15 10:08:42', 298486374)
825
+  (0.8ms) commit transaction
826
+  (0.0ms) begin transaction
827
+ ------------------------------------
828
+ ModelUtilsTest: test_reorder_forward
829
+ ------------------------------------
830
+  (0.1ms) rollback transaction
831
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
832
+  (0.1ms) begin transaction
833
+ Fixture Delete (0.8ms) DELETE FROM "books"
834
+ Fixture Insert (0.3ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('One', 'MyString', 'MyText', '2017-03-15 10:09:32', '2017-03-15 10:09:32', 980190962)
835
+ Fixture Insert (0.1ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 10:09:32', '2017-03-15 10:09:32', 298486374)
836
+  (0.7ms) commit transaction
837
+  (0.0ms) begin transaction
838
+ ------------------------------------
839
+ ModelUtilsTest: test_reorder_forward
840
+ ------------------------------------
841
+  (0.0ms) rollback transaction
842
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
843
+  (0.1ms) begin transaction
844
+ Fixture Delete (0.4ms) DELETE FROM "books"
845
+ Fixture Insert (0.2ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('One', 'MyString', 'MyText', '2017-03-15 10:11:23', '2017-03-15 10:11:23', 980190962)
846
+ Fixture Insert (0.1ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 10:11:23', '2017-03-15 10:11:23', 298486374)
847
+  (0.7ms) commit transaction
848
+  (0.1ms) begin transaction
849
+ ------------------------------------
850
+ ModelUtilsTest: test_reorder_forward
851
+ ------------------------------------
852
+  (0.1ms) rollback transaction
853
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
854
+  (0.1ms) begin transaction
855
+ Fixture Delete (0.4ms) DELETE FROM "books"
856
+ Fixture Insert (0.2ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('One', 'MyString', 'MyText', '2017-03-15 10:11:28', '2017-03-15 10:11:28', 980190962)
857
+ Fixture Insert (0.1ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 10:11:28', '2017-03-15 10:11:28', 298486374)
858
+  (0.8ms) commit transaction
859
+  (0.1ms) begin transaction
860
+ -------------------------------------
861
+ ModelUtilsTest: test_reorder_backward
862
+ -------------------------------------
863
+  (0.1ms) rollback transaction
864
+ ActiveRecord::SchemaMigration Load (0.6ms) SELECT "schema_migrations".* FROM "schema_migrations"
865
+  (0.3ms) begin transaction
866
+ Fixture Delete (1.1ms) DELETE FROM "books"
867
+ Fixture Insert (0.7ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('One', 'MyString', 'MyText', '2017-03-15 10:18:26', '2017-03-15 10:18:26', 980190962)
868
+ Fixture Insert (0.3ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 10:18:26', '2017-03-15 10:18:26', 298486374)
869
+  (1.5ms) commit transaction
870
+  (0.2ms) begin transaction
871
+ -------------------------------------
872
+ ModelUtilsTest: test_reorder_backward
873
+ -------------------------------------
874
+  (0.4ms) rollback transaction
875
+ ActiveRecord::SchemaMigration Load (0.5ms) SELECT "schema_migrations".* FROM "schema_migrations"
876
+  (0.3ms) begin transaction
877
+ Fixture Delete (0.8ms) DELETE FROM "books"
878
+ Fixture Insert (0.5ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('One', 'MyString', 'MyText', '2017-03-15 10:18:52', '2017-03-15 10:18:52', 980190962)
879
+ Fixture Insert (0.3ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 10:18:52', '2017-03-15 10:18:52', 298486374)
880
+  (1.2ms) commit transaction
881
+  (0.4ms) begin transaction
882
+ -------------------------------------
883
+ ModelUtilsTest: test_reorder_backward
884
+ -------------------------------------
885
+  (0.2ms) rollback transaction
886
+ ActiveRecord::SchemaMigration Load (0.7ms) SELECT "schema_migrations".* FROM "schema_migrations"
887
+  (0.3ms) begin transaction
888
+ Fixture Delete (0.7ms) DELETE FROM "books"
889
+ Fixture Insert (0.4ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('One', 'MyString', 'MyText', '2017-03-15 10:19:54', '2017-03-15 10:19:54', 980190962)
890
+ Fixture Insert (0.3ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 10:19:54', '2017-03-15 10:19:54', 298486374)
891
+  (1.1ms) commit transaction
892
+  (0.3ms) begin transaction
893
+ -------------------------------------
894
+ ModelUtilsTest: test_reorder_backward
895
+ -------------------------------------
896
+  (0.1ms) rollback transaction
897
+ ActiveRecord::SchemaMigration Load (0.6ms) SELECT "schema_migrations".* FROM "schema_migrations"
898
+  (0.1ms) begin transaction
899
+ Fixture Delete (0.7ms) DELETE FROM "books"
900
+ Fixture Insert (0.4ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('One', 'MyString', 'MyText', '2017-03-15 10:25:50', '2017-03-15 10:25:50', 980190962)
901
+ Fixture Insert (0.1ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 10:25:50', '2017-03-15 10:25:50', 298486374)
902
+  (0.9ms) commit transaction
903
+  (0.1ms) begin transaction
904
+ ----------------------------------------
905
+ ModelUtilsTest: test_reorder_forward_two
906
+ ----------------------------------------
907
+  (0.0ms) rollback transaction
908
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
909
+  (0.1ms) begin transaction
910
+ Fixture Delete (0.9ms) DELETE FROM "books"
911
+ Fixture Insert (0.3ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('One', 'MyString', 'MyText', '2017-03-15 10:26:03', '2017-03-15 10:26:03', 980190962)
912
+ Fixture Insert (0.1ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 10:26:03', '2017-03-15 10:26:03', 298486374)
913
+  (8.5ms) commit transaction
914
+  (0.1ms) begin transaction
915
+ ----------------------------------------
916
+ ModelUtilsTest: test_reorder_forward_two
917
+ ----------------------------------------
918
+  (0.1ms) rollback transaction
919
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
920
+  (0.2ms) begin transaction
921
+ Fixture Delete (0.7ms) DELETE FROM "books"
922
+ Fixture Insert (0.3ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('One', 'MyString', 'MyText', '2017-03-15 10:26:17', '2017-03-15 10:26:17', 980190962)
923
+ Fixture Insert (0.1ms) INSERT INTO "books" ("title", "author", "summary", "created_at", "updated_at", "id") VALUES ('MyString', 'MyString', 'MyText', '2017-03-15 10:26:17', '2017-03-15 10:26:17', 298486374)
924
+  (8.5ms) commit transaction
925
+  (0.1ms) begin transaction
926
+ ----------------------------------------
927
+ ModelUtilsTest: test_reorder_forward_two
928
+ ----------------------------------------
929
+  (0.1ms) rollback transaction
@@ -1,7 +1,7 @@
1
1
  # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2
2
 
3
3
  one:
4
- title: MyString
4
+ title: One
5
5
  author: MyString
6
6
  summary: MyText
7
7
 
File without changes
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class MiscUtilsTest < ActiveSupport::TestCase
4
+ def test_rescue_mysql2_error
5
+ e = RuntimeError.new("Incorrect string value:")
6
+ Librails::MiscUtils.rescue_mysql2_error(e)
7
+ end
8
+ end
@@ -0,0 +1,62 @@
1
+ require 'test_helper'
2
+
3
+ class ModelUtilsTest < ActiveSupport::TestCase
4
+ class DummyItem
5
+ attr_accessor :id, :disp_order
6
+ end
7
+
8
+ def create_dummy_items
9
+ i1 = DummyItem.new
10
+ i1.id = 10
11
+ i2 = DummyItem.new
12
+ i2.id = 20
13
+ i3 = DummyItem.new
14
+ i3.id = 30
15
+ i4 = DummyItem.new
16
+ i4.id = 40
17
+ [i1, i2, i3, i4]
18
+ end
19
+
20
+ def dummy_items_to_s(items)
21
+ items.collect{|item| item.id.to_s + '-' + item.disp_order.to_s}.join(',')
22
+ end
23
+
24
+ def test_reorder_forward
25
+ # 前方へ入れ替え
26
+ hash = {
27
+ '10' => {'disp_order' => '1'},
28
+ '20' => {'disp_order' => '2'},
29
+ '30' => {'disp_order' => '1'},
30
+ '40' => {'disp_order' => '4'},
31
+ }
32
+ items = create_dummy_items
33
+ Librails::ModelUtils.reorder(items, hash)
34
+ assert_equal('30-1,10-2,20-3,40-4', dummy_items_to_s(items))
35
+ end
36
+
37
+ def test_reorder_backward
38
+ # 後方へ入れ替え
39
+ hash = {
40
+ '10' => {'disp_order' => '2'},
41
+ '20' => {'disp_order' => '2'},
42
+ '30' => {'disp_order' => '3'},
43
+ '40' => {'disp_order' => '4'},
44
+ }
45
+ items = create_dummy_items
46
+ Librails::ModelUtils.reorder(items, hash)
47
+ assert_equal('20-1,10-2,30-3,40-4', dummy_items_to_s(items))
48
+ end
49
+
50
+ def test_reorder_forward_two
51
+ # 前方へ2つ入れ替え
52
+ hash = {
53
+ '10' => {'disp_order' => '1'},
54
+ '20' => {'disp_order' => '2'},
55
+ '30' => {'disp_order' => '2'},
56
+ '40' => {'disp_order' => '1'},
57
+ }
58
+ items = create_dummy_items
59
+ Librails::ModelUtils.reorder(items, hash)
60
+ assert_equal('40-1,30-2,10-3,20-4', dummy_items_to_s(items))
61
+ end
62
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class BookTest < ActiveSupport::TestCase
4
+ def test_book
5
+ book = books(:one)
6
+ assert_equal('One', book.title)
7
+ end
8
+ end
metadata CHANGED
@@ -1,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: librails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - src
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-26 00:00:00.000000000 Z
11
+ date: 2017-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ - - "<"
18
21
  - !ruby/object:Gem::Version
19
- version: 4.2.7
22
+ version: '6'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '4.0'
30
+ - - "<"
25
31
  - !ruby/object:Gem::Version
26
- version: 4.2.7
32
+ version: '6'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: sqlite3
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -59,6 +65,7 @@ files:
59
65
  - lib/librails.rb
60
66
  - lib/librails/engine.rb
61
67
  - lib/librails/misc_utils.rb
68
+ - lib/librails/model_utils.rb
62
69
  - lib/librails/version.rb
63
70
  - lib/librails/view_helper.rb
64
71
  - lib/tasks/librails_tasks.rake
@@ -188,7 +195,10 @@ files:
188
195
  - test/fixtures/books.yml
189
196
  - test/integration/books_controller_test.rb
190
197
  - test/integration/navigation_test.rb
191
- - test/librails_test.rb
198
+ - test/lib/librails_test.rb
199
+ - test/lib/misc_utils_test.rb
200
+ - test/lib/model_utils_test.rb
201
+ - test/models/book_test.rb
192
202
  - test/test_helper.rb
193
203
  homepage: ''
194
204
  licenses:
@@ -210,7 +220,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
210
220
  version: '0'
211
221
  requirements: []
212
222
  rubyforge_project:
213
- rubygems_version: 2.5.1
223
+ rubygems_version: 2.6.8
214
224
  signing_key:
215
225
  specification_version: 4
216
226
  summary: My Rails Plugin
@@ -341,5 +351,8 @@ test_files:
341
351
  - test/fixtures/books.yml
342
352
  - test/integration/books_controller_test.rb
343
353
  - test/integration/navigation_test.rb
344
- - test/librails_test.rb
354
+ - test/lib/librails_test.rb
355
+ - test/lib/misc_utils_test.rb
356
+ - test/lib/model_utils_test.rb
357
+ - test/models/book_test.rb
345
358
  - test/test_helper.rb