form_core 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5445ceba04d699e653eb979f4bbb4d47b2a02d35
4
+ data.tar.gz: 43378b5c8cc8ece05bf173e942a124edda795595
5
+ SHA512:
6
+ metadata.gz: 054dd0d3d050c6700bea922a5fe6d672fae0b18e429869808da7576ea98b894e8be4f8dade8b625970ab68e319e9078e423068a76c120722e79427939f1158c7
7
+ data.tar.gz: de14cd9112daf2557a8548face1551591b8eaa8e4ab157b24306f35d1aaa59d16c6475761cadf9a0e4551320c5a14cf942ce981984395c3cf33d363abe8289d5
@@ -0,0 +1,20 @@
1
+ Copyright 2017 Jun Jiang
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,98 @@
1
+ Form Core
2
+ ====
3
+
4
+ A Rails engine provides ability to generate dynamic form.
5
+
6
+ ## Usage
7
+
8
+ TODO
9
+
10
+ ## Installation
11
+
12
+ Add this line to your Gemfile:
13
+
14
+ ```ruby
15
+ gem 'form_core'
16
+ ```
17
+
18
+ Or you may want to include the gem directly from GitHub:
19
+
20
+ ```ruby
21
+ gem 'form_core', github: 'jasl-lab/form_core'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ ```sh
27
+ $ bundle
28
+ ```
29
+
30
+ Copy migrations
31
+
32
+ ```sh
33
+ $ bin/rails form_core:install:migrations
34
+ ```
35
+
36
+ Then do migrate
37
+
38
+ ```sh
39
+ $ bin/rails db:migrate
40
+ ```
41
+
42
+ ## Demo
43
+
44
+ Clone the repository.
45
+
46
+ ```sh
47
+ $ git clone https://github.com/jasl/form_core.git
48
+ ```
49
+
50
+ Change directory
51
+
52
+ ```sh
53
+ $ cd form_core
54
+ ```
55
+
56
+ Run bundler
57
+
58
+ ```sh
59
+ $ bundle install
60
+ ```
61
+
62
+ Run yarn
63
+
64
+ ```sh
65
+ $ test/dummy/bin/yarn
66
+ ```
67
+
68
+ Preparing database
69
+
70
+ ```sh
71
+ $ bin/rails db:migrate
72
+ ```
73
+
74
+ Start the Rails server
75
+
76
+ ```sh
77
+ $ bin/rails s
78
+ ```
79
+
80
+ Open your browser, and visit `http://localhost:3000`
81
+
82
+ ## Contributing
83
+
84
+ Bug report or pull request are welcome.
85
+
86
+ ### Make a pull request
87
+
88
+ 1. Fork it
89
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
90
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
91
+ 4. Push to the branch (`git push origin my-new-feature`)
92
+ 5. Create new Pull Request
93
+
94
+ Please write unit test with your code if necessary.
95
+
96
+ ## License
97
+
98
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,36 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'FormCore'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ require 'bundler/gem_tasks'
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'test'
31
+ t.pattern = 'test/**/*_test.rb'
32
+ t.verbose = false
33
+ end
34
+
35
+
36
+ task default: :test
@@ -0,0 +1,5 @@
1
+ module FormCore
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module FormCore
2
+ class Field < ApplicationRecord
3
+ include FormCore::Concerns::Models::Field
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module FormCore
2
+ class Form < ApplicationRecord
3
+ include FormCore::Concerns::Models::Form
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ class CreateFormCoreForms < ActiveRecord::Migration[5.1]
2
+ def change
3
+ create_table :form_core_forms do |t|
4
+ t.string :type, null: false, index: true
5
+
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ class CreateFormCoreFields < ActiveRecord::Migration[5.1]
2
+ def change
3
+ create_table :form_core_fields do |t|
4
+ t.string :name, null: false
5
+ t.integer :accessibility, null: false
6
+ t.text :validations
7
+ t.text :static_default_value
8
+ t.text :options
9
+ t.string :type, null: false
10
+ t.references :form, foreign_key: {to_table: :form_core_forms}
11
+
12
+ t.timestamps
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,46 @@
1
+ require "form_core/engine"
2
+ require "form_core/virtual_model"
3
+
4
+ require "form_core/concerns/models/form"
5
+ require "form_core/concerns/models/field"
6
+
7
+ module FormCore
8
+ class << self
9
+ def field_classes
10
+ @field_classes ||= Set.new
11
+ end
12
+
13
+ def register_field_class(klass)
14
+ unless klass && klass < Field && !klass.abstract_class?
15
+ raise ArgumentError, "#{klass} should be sub-class of #{Field} and can't be abstract class."
16
+ end
17
+
18
+ field_classes << klass
19
+ end
20
+
21
+ def register_field_classes(*classes)
22
+ classes.each do |klass|
23
+ register_field_class klass
24
+ end
25
+ end
26
+
27
+ def virtual_model_class
28
+ @virtual_model_class ||= VirtualModel
29
+ end
30
+
31
+ def virtual_model_class=(klass)
32
+ unless klass && klass < VirtualModel
33
+ raise ArgumentError, "#{klass} should be sub-class of #{VirtualModel}."
34
+ end
35
+
36
+ @virtual_model_class = klass
37
+ end
38
+
39
+ def reserved_names
40
+ @reserved_names ||= Set.new(
41
+ %i(def class module private public protected allocate new parent superclass) +
42
+ virtual_model_class.instance_methods(true)
43
+ )
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,87 @@
1
+ module FormCore::Concerns
2
+ module Models
3
+ module Field
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ belongs_to :form, touch: true
8
+
9
+ enum accessibility: {read_and_write: 0, read_only: 1, hidden: 2}
10
+
11
+ serialize :validations
12
+ serialize :options
13
+ serialize :static_default_value
14
+
15
+ validates :name,
16
+ presence: true,
17
+ uniqueness: {scope: :form},
18
+ exclusion: {in: FormCore.reserved_names},
19
+ format: {with: /\A[a-z0-9_]+\z/}
20
+ validates :accessibility,
21
+ inclusion: {in: self.accessibilities.keys.map(&:to_sym)}
22
+
23
+ after_initialize do
24
+ self.validations ||= {}
25
+ self.options ||= {}
26
+ self.accessibility ||= "read_and_write"
27
+ end
28
+ end
29
+
30
+ def name
31
+ super&.to_sym
32
+ end
33
+
34
+ def accessibility
35
+ super&.to_sym
36
+ end
37
+
38
+ def stored_type
39
+ raise NotImplementedError
40
+ end
41
+
42
+ def default_value
43
+ ActiveModel::Type.lookup(stored_type).cast(static_default_value)
44
+ end
45
+
46
+ def interpret_to(model, overrides: {})
47
+ check_model_validity!(model)
48
+
49
+ accessibility = overrides.fetch(:accessibility, self.accessibility)
50
+ return model if accessibility == :hidden
51
+
52
+ default_value = overrides.fetch(:default_value, self.default_value)
53
+ model.attribute name, stored_type, default: default_value
54
+
55
+ if accessibility == :read_only
56
+ model.attr_readonly name
57
+ end
58
+
59
+ interpret_validations_to model, accessibility, overrides
60
+ interpret_extra_to model, accessibility, overrides
61
+
62
+ model
63
+ end
64
+
65
+ protected
66
+
67
+ def interpret_validations_to(model, accessibility, overrides = {})
68
+ validations = overrides.fetch(:validations, (self.validations || {}))
69
+ validation_options = overrides.fetch(:validation_options) { self.options.fetch(:validation, {}) }
70
+
71
+ if accessibility == :read_and_write && validations.present?
72
+ model.validates name, **validations, **validation_options
73
+ end
74
+ end
75
+
76
+ def interpret_extra_to(_model, _accessibility, _overrides = {})
77
+
78
+ end
79
+
80
+ def check_model_validity!(model)
81
+ unless model.is_a?(Class) && model < ::FormCore::VirtualModel
82
+ raise ArgumentError, "#{model} must be a #{::FormCore::VirtualModel}'s subclass"
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,26 @@
1
+ module FormCore::Concerns
2
+ module Models
3
+ module Form
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ has_many :fields
8
+ end
9
+
10
+ DEFAULT_MODEL_NAME = "Form"
11
+ DEFAULT_FIELDS_SCOPE = proc { |fields| fields }
12
+ def to_virtual_model(model_name: DEFAULT_MODEL_NAME,
13
+ fields_scope: DEFAULT_FIELDS_SCOPE,
14
+ overrides: {})
15
+ model = Class.new(FormCore.virtual_model_class)
16
+ model.model_name = model_name
17
+
18
+ fields_scope.call(fields).each do |f|
19
+ f.interpret_to model, overrides: overrides.fetch(f.name, {})
20
+ end
21
+
22
+ model
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ module FormCore
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace FormCore
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module FormCore
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,12 @@
1
+ require "duck_record"
2
+
3
+ module FormCore
4
+ class VirtualModel < ::DuckRecord::Base
5
+ public_class_method :define_method
6
+ class << self
7
+ def model_name=(name)
8
+ @_model_name = ActiveModel::Name.new(self, nil, name.classify)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :form_core do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: form_core
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - jasl
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: duck_record
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: |
56
+ A Rails engine provides ability to generate dynamic form.
57
+ It's would make such as dynamic fields of model or questionnaire easily.
58
+ email:
59
+ - jasl9187@hotmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - MIT-LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - app/models/form_core/application_record.rb
68
+ - app/models/form_core/field.rb
69
+ - app/models/form_core/form.rb
70
+ - db/migrate/20170430190404_create_form_core_forms.rb
71
+ - db/migrate/20170430191336_create_form_core_fields.rb
72
+ - lib/form_core.rb
73
+ - lib/form_core/concerns/models/field.rb
74
+ - lib/form_core/concerns/models/form.rb
75
+ - lib/form_core/engine.rb
76
+ - lib/form_core/version.rb
77
+ - lib/form_core/virtual_model.rb
78
+ - lib/tasks/form_core_tasks.rake
79
+ homepage: https://github.com/jasl-lab/form_core
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.6.11
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: A Rails engine provides ability to generate dynamic form.
103
+ test_files: []