application_form 0.4.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 05c6c488f4f25253c2fb3889eda3776207218a8a1bfb7101b18e9fc38fc5e663
4
+ data.tar.gz: e8c073b39558f58391202b6d7353cc4be57e7c4df01987f31b1b624012fd010f
5
+ SHA512:
6
+ metadata.gz: e174c55c53544fd420808519c705f546fe166a44965c279a09ac25ccbd52c47e09329c06031aa3f1e143987400678322f870fa5736717f68c7397f7579097b86
7
+ data.tar.gz: d1f64700776c503711dc40a6ee4530963058cac534bfd49473a5567c01b2bda117b375f223eeedb434196ee3fb2275ca35db7ad6c165f7ee3da14138383f6552
@@ -0,0 +1,19 @@
1
+ name: main
2
+ on: [push, pull_request]
3
+ jobs:
4
+ test:
5
+ strategy:
6
+ fail-fast: true
7
+ matrix:
8
+ os: [ubuntu-latest, macos-latest]
9
+ # Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
10
+ ruby: [2.5, 2.6, 2.7, '3.0', head]
11
+ runs-on: ${{ matrix.os }}
12
+ steps:
13
+ - uses: actions/checkout@v2
14
+ - uses: ruby/setup-ruby@v1
15
+ with:
16
+ ruby-version: ${{ matrix.ruby }}
17
+ bundler-cache: true
18
+ - name: run test
19
+ run: make test
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
data/.rubocop.yml ADDED
@@ -0,0 +1,10 @@
1
+ # The behavior of RuboCop can be controlled via the .rubocop.yml
2
+ # configuration file. It makes it possible to enable/disable
3
+ # certain cops (checks) and to alter their behavior if they accept
4
+ # any parameters. The file can be placed either in your home
5
+ # directory or in some project directory.
6
+ #
7
+ # RuboCop will start looking for the configuration file in the directory
8
+ # where the inspected file is and continue its way up to the root directory.
9
+ #
10
+ # See https://docs.rubocop.org/rubocop/configuration
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at mokevnin@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in application_form.gemspec
6
+ gemspec
7
+
8
+ gem 'minitest', '~> 5.0'
9
+ gem 'rake', '~> 12.0'
10
+
11
+ gem 'rubocop', '~> 1.18'
12
+
13
+ gem "minitest-power_assert", "~> 0.3.1"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 mokevnin
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/Makefile ADDED
@@ -0,0 +1,19 @@
1
+ install:
2
+ bin/setup
3
+
4
+ console:
5
+ bin/console
6
+
7
+ test:
8
+ bundle exec rake test
9
+
10
+ lint:
11
+ bundle exec rubocop -A
12
+
13
+ release:
14
+ bundle exec rake release
15
+
16
+ build:
17
+ bundle exec rake build
18
+
19
+ .PHONY: test
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # ApplicationForm
2
+
3
+ ![github action status](https://github.com/rbbr_io/application_form/workflows/main/badge.svg)
4
+
5
+ Painless forms for ActiveRecord. Based on Inheritance. Included:
6
+
7
+ * Strong parameters
8
+ * Validation (based on the model validation)
9
+ * Data normalization
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'application_form'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle install
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install application_form
26
+
27
+ ## Usage
28
+
29
+ 1. Create directory *app/forms*
30
+ 1. Add Form class for a model
31
+ 1. Add permitted params inside the class
32
+ 1. Use it as a normal model (without strong_params)
33
+
34
+ ## Generator
35
+
36
+ Use the supplied generator to generate forms:
37
+
38
+ $ rails g application_form:form sign_up --model=user
39
+
40
+ or with namespace model
41
+
42
+ $ rails g application_form:form admin_post --model=blog/post
43
+
44
+ ### Example
45
+
46
+ ```ruby
47
+ # app/forms/user_sign_up_form.rb
48
+ class UserSignUpForm < User
49
+ include ApplicationForm
50
+
51
+ # list all the permitted params
52
+ permit :first_name, :email, :password
53
+
54
+ # add validation if necessary
55
+ # they will be merged with base class' validation
56
+ validates :password, presence: true
57
+
58
+ # optional data normalization
59
+ def email=(email)
60
+ if email.present?
61
+ write_attribute(:email, email.downcase)
62
+ else
63
+ super
64
+ end
65
+ end
66
+ end
67
+ ```
68
+
69
+ In some cases it is necessary to use ActiveRecord object directly without form. For such cases conveniently to use method `become()` (built-in ActiveRecord):
70
+
71
+ ```ruby
72
+ user = User.find(params[:id])
73
+ form = user.becomes(UserSignUpForm)
74
+ ```
75
+
76
+ ## Development
77
+
78
+ 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.
79
+
80
+ 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).
81
+
82
+ ## Contributing
83
+
84
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rbbr_io/application_form. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/rbbr_io/application_form/blob/master/CODE_OF_CONDUCT.md).
85
+
86
+
87
+ ## License
88
+
89
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
90
+
91
+ ## Code of Conduct
92
+
93
+ Everyone interacting in the ApplicationForm project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/rbbr_io/application_form/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/application_form/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'application_form'
7
+ spec.version = ApplicationForm::VERSION
8
+ spec.authors = ['Kirill Mokevnin', 'Ivan Nemytchenko']
9
+ spec.email = ['nemytchenko@gmail.com']
10
+
11
+ spec.summary = 'Painless forms for ActiveRecord'
12
+ spec.description = 'Lightweight inheritance based forms solution'
13
+ spec.homepage = 'https://github.com/rbbr_io/application_form'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = 'https://github.com/rbbr_io/application_form.git'
19
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_dependency('activesupport', '>= 3')
31
+
32
+ spec.add_development_dependency('actionpack', '>= 5')
33
+ spec.add_development_dependency('activemodel', '>= 5')
34
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'application_form'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ 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,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'application_form/version'
4
+ require 'active_support/concern'
5
+
6
+ module ApplicationForm
7
+ class Error < StandardError; end
8
+
9
+ extend ActiveSupport::Concern
10
+
11
+ included do
12
+ prepend Included
13
+ # raise superclass.inspect
14
+ # parent.extend ActiveModel::Naming
15
+ end
16
+
17
+ module Included
18
+ def initialize(attrs = {})
19
+ permitted_attrs = permit_attrs(attrs)
20
+ super(permitted_attrs)
21
+ end
22
+ end
23
+
24
+ module ClassMethods
25
+ delegate :sti_name, to: :superclass
26
+ delegate :human_attribute_name, to: :superclass
27
+ # NOTE: too many side effects if it is enabled
28
+ # examples: form names, translations
29
+ # delegate :name, to: :superclass
30
+
31
+ def permit(*args)
32
+ @_permitted_args = args
33
+ end
34
+
35
+ def _permitted_args
36
+ @_permitted_args || (superclass.respond_to?(:_permitted_args) && superclass._permitted_args) || []
37
+ end
38
+ end
39
+
40
+ def update(attrs = {})
41
+ permitted_attrs = permit_attrs(attrs)
42
+ super(permitted_attrs)
43
+ end
44
+
45
+ def update!(attrs = {})
46
+ permitted_attrs = permit_attrs(attrs)
47
+ super(permitted_attrs)
48
+ end
49
+
50
+ def assign_attributes(attrs = {})
51
+ permitted_attrs = permit_attrs(attrs)
52
+ super(permitted_attrs)
53
+ end
54
+
55
+ def permit_attrs(attrs)
56
+ attrs.respond_to?(:permit) ? attrs.send(:permit, self.class._permitted_args) : attrs
57
+ end
58
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApplicationForm
4
+ VERSION = '0.4.0'
5
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Generates a form for a model with the given name.
3
+
4
+ Example:
5
+ rails generate application_form:form sign_up --model=user
6
+
7
+ This will create:
8
+ app/forms/user_form.rb
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApplicationForm
4
+ module Generators
5
+ class FormGenerator < ::Rails::Generators::NamedBase
6
+ class_option :model, type: :string
7
+ source_root File.expand_path('templates', __dir__)
8
+
9
+ def create_form
10
+ template 'form.rb', File.join('app/forms', class_path, "#{file_name}_form.rb")
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ <% module_namespacing do -%>
2
+ class <%= class_name %>Form < <%= options['model'].classify %>
3
+ include ApplicationForm
4
+ end
5
+ <% end -%>
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: application_form
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Kirill Mokevnin
8
+ - Ivan Nemytchenko
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2021-08-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '3'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '3'
28
+ - !ruby/object:Gem::Dependency
29
+ name: actionpack
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '5'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '5'
42
+ - !ruby/object:Gem::Dependency
43
+ name: activemodel
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '5'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '5'
56
+ description: Lightweight inheritance based forms solution
57
+ email:
58
+ - nemytchenko@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".github/workflows/main.yml"
64
+ - ".gitignore"
65
+ - ".rubocop.yml"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - Makefile
70
+ - README.md
71
+ - Rakefile
72
+ - application_form.gemspec
73
+ - bin/console
74
+ - bin/setup
75
+ - lib/application_form.rb
76
+ - lib/application_form/version.rb
77
+ - lib/generators/application_form/USAGE
78
+ - lib/generators/application_form/form_generator.rb
79
+ - lib/generators/application_form/templates/form.rb
80
+ homepage: https://github.com/rbbr_io/application_form
81
+ licenses:
82
+ - MIT
83
+ metadata:
84
+ homepage_uri: https://github.com/rbbr_io/application_form
85
+ source_code_uri: https://github.com/rbbr_io/application_form.git
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 2.3.0
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubygems_version: 3.1.4
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Painless forms for ActiveRecord
105
+ test_files: []