super_tools 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c94b8129949bb21f865c7922da190e08f0c3db6a
4
+ data.tar.gz: 069a2ba8bf61de41bfed793c5291a6b024297f12
5
+ SHA512:
6
+ metadata.gz: 0d00323559efdd468cb1223795284b28c9fc6c5ff563e3c3c1204b5dd91daf3108858ca0153cd6470bc1d10553576e46c502e6be746da054c26a312517b593d9
7
+ data.tar.gz: 455e3c83a3cc86acd1dee5d62a1a0ce9aaa5147ad84e2164ad2461a3543d5fdb47c0dc179ee7f17617ff803701c78b27c6a6f7628811a57f213b34fe02f58a20
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.2.5
7
+ before_install: gem install bundler -v 1.16.6
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in super_tools.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,173 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ super_tools (0.0.1)
5
+ actionview
6
+ activemodel
7
+ activerecord
8
+ activeview
9
+ dry-logic (~> 0.4.2)
10
+ dry-types (~> 0.13.2)
11
+ fast_excel
12
+ forwardable
13
+ iconv
14
+ reform (~> 2.2.4)
15
+ reform-rails
16
+ roo
17
+ roo-xls
18
+ spreadsheet
19
+ virtus (~> 1.0.5)
20
+ warehouse_items (~> 0.2.0)
21
+
22
+ GEM
23
+ remote: https://rubygems.org/
24
+ specs:
25
+ actionpack (5.2.3)
26
+ actionview (= 5.2.3)
27
+ activesupport (= 5.2.3)
28
+ rack (~> 2.0)
29
+ rack-test (>= 0.6.3)
30
+ rails-dom-testing (~> 2.0)
31
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
32
+ actionview (5.2.3)
33
+ activesupport (= 5.2.3)
34
+ builder (~> 3.1)
35
+ erubi (~> 1.4)
36
+ rails-dom-testing (~> 2.0)
37
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
38
+ activemodel (5.2.3)
39
+ activesupport (= 5.2.3)
40
+ activerecord (5.2.3)
41
+ activemodel (= 5.2.3)
42
+ activesupport (= 5.2.3)
43
+ arel (>= 9.0)
44
+ activesupport (5.2.3)
45
+ concurrent-ruby (~> 1.0, >= 1.0.2)
46
+ i18n (>= 0.7, < 2)
47
+ minitest (~> 5.1)
48
+ tzinfo (~> 1.1)
49
+ activeview (0)
50
+ activesupport (~> 5.0)
51
+ railties (~> 5.0)
52
+ arel (9.0.0)
53
+ axiom-types (0.1.1)
54
+ descendants_tracker (~> 0.0.4)
55
+ ice_nine (~> 0.11.0)
56
+ thread_safe (~> 0.3, >= 0.3.1)
57
+ builder (3.2.3)
58
+ coercible (1.0.0)
59
+ descendants_tracker (~> 0.0.1)
60
+ concurrent-ruby (1.1.5)
61
+ crass (1.0.4)
62
+ declarative (0.0.10)
63
+ declarative-builder (0.1.0)
64
+ declarative-option (< 0.2.0)
65
+ declarative-option (0.1.0)
66
+ descendants_tracker (0.0.4)
67
+ thread_safe (~> 0.3, >= 0.3.1)
68
+ disposable (0.4.4)
69
+ declarative (>= 0.0.9, < 1.0.0)
70
+ declarative-builder (< 0.2.0)
71
+ declarative-option (< 0.2.0)
72
+ representable (>= 2.4.0, <= 3.1.0)
73
+ uber (< 0.2.0)
74
+ dry-configurable (0.7.0)
75
+ concurrent-ruby (~> 1.0)
76
+ dry-container (0.6.0)
77
+ concurrent-ruby (~> 1.0)
78
+ dry-configurable (~> 0.1, >= 0.1.3)
79
+ dry-core (0.4.7)
80
+ concurrent-ruby (~> 1.0)
81
+ dry-equalizer (0.2.2)
82
+ dry-inflector (0.1.2)
83
+ dry-logic (0.4.2)
84
+ dry-container (~> 0.2, >= 0.2.6)
85
+ dry-core (~> 0.2)
86
+ dry-equalizer (~> 0.2)
87
+ dry-types (0.13.4)
88
+ concurrent-ruby (~> 1.0)
89
+ dry-container (~> 0.3)
90
+ dry-core (~> 0.4, >= 0.4.4)
91
+ dry-equalizer (~> 0.2)
92
+ dry-inflector (~> 0.1, >= 0.1.2)
93
+ dry-logic (~> 0.4, >= 0.4.2)
94
+ equalizer (0.0.11)
95
+ erubi (1.8.0)
96
+ fast_excel (0.2.6)
97
+ ffi (> 1.9, < 2)
98
+ ffi (1.10.0)
99
+ forwardable (1.2.0)
100
+ i18n (1.5.1)
101
+ concurrent-ruby (~> 1.0)
102
+ ice_nine (0.11.2)
103
+ iconv (1.0.8)
104
+ loofah (2.2.3)
105
+ crass (~> 1.0.2)
106
+ nokogiri (>= 1.5.9)
107
+ method_source (0.9.2)
108
+ mini_portile2 (2.4.0)
109
+ minitest (5.11.3)
110
+ nokogiri (1.9.1)
111
+ mini_portile2 (~> 2.4.0)
112
+ rack (2.0.7)
113
+ rack-test (1.1.0)
114
+ rack (>= 1.0, < 3)
115
+ rails-dom-testing (2.0.3)
116
+ activesupport (>= 4.2.0)
117
+ nokogiri (>= 1.6)
118
+ rails-html-sanitizer (1.0.4)
119
+ loofah (~> 2.2, >= 2.2.2)
120
+ railties (5.2.3)
121
+ actionpack (= 5.2.3)
122
+ activesupport (= 5.2.3)
123
+ method_source
124
+ rake (>= 0.8.7)
125
+ thor (>= 0.19.0, < 2.0)
126
+ rake (10.5.0)
127
+ reform (2.2.4)
128
+ disposable (>= 0.4.1)
129
+ representable (>= 2.4.0, < 3.1.0)
130
+ reform-rails (0.1.7)
131
+ activemodel (>= 3.2)
132
+ reform (>= 2.2.0)
133
+ representable (3.0.4)
134
+ declarative (< 0.1.0)
135
+ declarative-option (< 0.2.0)
136
+ uber (< 0.2.0)
137
+ roo (2.7.1)
138
+ nokogiri (~> 1)
139
+ rubyzip (~> 1.1, < 2.0.0)
140
+ roo-xls (1.2.0)
141
+ nokogiri
142
+ roo (>= 2.0.0, < 3)
143
+ spreadsheet (> 0.9.0)
144
+ ruby-ole (1.2.12.2)
145
+ rubyzip (1.2.2)
146
+ shoulda-context (1.2.2)
147
+ spreadsheet (1.2.3)
148
+ ruby-ole (>= 1.0)
149
+ thor (0.20.3)
150
+ thread_safe (0.3.6)
151
+ tzinfo (1.2.5)
152
+ thread_safe (~> 0.1)
153
+ uber (0.1.0)
154
+ virtus (1.0.5)
155
+ axiom-types (~> 0.1)
156
+ coercible (~> 1.0)
157
+ descendants_tracker (~> 0.0, >= 0.0.3)
158
+ equalizer (~> 0.0, >= 0.0.9)
159
+ warehouse_items (0.2.0)
160
+ activesupport
161
+
162
+ PLATFORMS
163
+ ruby
164
+
165
+ DEPENDENCIES
166
+ bundler (~> 1.16)
167
+ minitest (~> 5.0)
168
+ rake (~> 10.0)
169
+ shoulda-context
170
+ super_tools!
171
+
172
+ BUNDLED WITH
173
+ 1.16.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 eddie
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # SuperTools
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/super_tools`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'super_tools'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install super_tools
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/super_tools.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:t) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "super_tools"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,43 @@
1
+ require 'virtus'
2
+ module SuperForm
3
+ class Basic
4
+ include ActiveModel::Model
5
+ extend ActiveModel::Naming
6
+ include ActiveModel::Conversion
7
+ include ActiveModel::Validations
8
+ include Virtus.model
9
+
10
+ def sync_params(params)
11
+ params.each do |attr, value|
12
+ public_send("#{attr}=", value) if respond_to?("#{attr}=")
13
+ end
14
+ end
15
+
16
+ class << self
17
+ def form_name(name)
18
+ # 定義 form name (給 form 用的)
19
+ define_singleton_method :model_name do
20
+ active_model_name_for(name.to_s.camelize)
21
+ end
22
+ end
23
+
24
+ # default is forms
25
+ def i18n_scope
26
+ :forms
27
+ end
28
+
29
+ # 定義 i18n scope
30
+ def i18n_prefix(i18n_scope)
31
+ define_singleton_method :i18n_scope do
32
+ i18n_scope.to_sym
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def active_model_name_for(string)
39
+ ::ActiveModel::Name.new(self, nil, string)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,70 @@
1
+ module SuperForm
2
+ module AtomicSave
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ include ActiveSupport::Callbacks
7
+
8
+ define_callbacks :transaction, :queries, :validations
9
+
10
+ def self.before_transaction(method_name)
11
+ set_callback :transaction, :before, method_name
12
+ end
13
+
14
+ def self.before_queries(method_name)
15
+ set_callback :queries, :before, method_name
16
+ end
17
+
18
+ def self.before_commit(method_name)
19
+ set_callback :queries, :after, method_name
20
+ end
21
+
22
+ def self.after_commit(method_name)
23
+ set_callback :transaction, :after, method_name
24
+ end
25
+
26
+ def self.before_validations(method_name)
27
+ set_callback :validations, :after, method_name
28
+ end
29
+
30
+ def self.after_validations(method_name)
31
+ set_callback :validations, :after, method_name
32
+ end
33
+ end
34
+
35
+ ReformAtomicSaveError = Class.new(StandardError)
36
+
37
+ def save_with_transaction(params={}, &block)
38
+ begin
39
+ save_with_transaction!(params, &block)
40
+ rescue ReformAtomicSaveError
41
+ false
42
+ end
43
+ end
44
+
45
+ def save_with_transaction!(params={}, &block)
46
+ run_callbacks :transaction do
47
+ ActiveRecord::Base.transaction do
48
+ run_callbacks :queries do
49
+ # 1. do validate data
50
+ run_callbacks :validations do
51
+ if validate(params) == false
52
+ raise ReformAtomicSaveError, "Not Validate => #{self.errors.full_messages.inspect}"
53
+ end
54
+ end
55
+
56
+ # 2. sync to model
57
+ sync
58
+
59
+ # 2. define how to store data in block
60
+ yield if block_given?
61
+
62
+ # 3. save all data
63
+ save!
64
+ end
65
+ end
66
+ end
67
+ true
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,46 @@
1
+ require 'super_form/concerns/atomic_save'
2
+ require 'disposable'
3
+ require 'reform'
4
+ require 'reform/form'
5
+ require 'reform/form/active_record'
6
+ require 'reform/form/active_model'
7
+ require 'reform/form/active_model/form_builder_methods'
8
+ require "reform/form/active_model/validations"
9
+ require "reform/form/coercion"
10
+ require 'disposable/twin/parent'
11
+
12
+ Reform::Form.class_eval do
13
+ extend ActiveModel::Naming
14
+ extend ActiveModel::Translation
15
+ end
16
+
17
+ class SuperForm::Reform < Reform::Form
18
+ include SuperForm::AtomicSave
19
+ include ::Reform::Form::ActiveRecord
20
+ include ::Reform::Form::ActiveModel
21
+ include ::Reform::Form::ActiveModel::FormBuilderMethods
22
+ feature Coercion
23
+
24
+ # 定義 i18n scope
25
+ def self.i18n_prefix(i18n_scope)
26
+ define_singleton_method :i18n_scope do
27
+ i18n_scope.to_sym
28
+ end
29
+ end
30
+
31
+ self.i18n_prefix :activerecord
32
+
33
+ def self.form_name(name)
34
+ # 定義 form name (給 form 用的)
35
+ define_singleton_method :model_name do
36
+ active_model_name_for(name.to_s.camelize)
37
+ end
38
+ model(name)
39
+ end
40
+
41
+ private
42
+
43
+ def self.active_model_name_for(string)
44
+ ::ActiveModel::Name.new(self, nil, string)
45
+ end
46
+ end