lite-form 1.0.0

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
+ SHA256:
3
+ metadata.gz: 0a8ca7a4757adcecac09d4d977dbd354cdb9215f96a573ce9922e44602293f68
4
+ data.tar.gz: 2ef1338ab530d735be39b77c8ce4e870bf8ed842d315635bd71475cd9e1aa1a3
5
+ SHA512:
6
+ metadata.gz: f3a33e566908020673daa66c4c0c91b596f791362fab0fe57ab7ea3f44718193bc790b8b20e40f9b5598410704d17e1574e4063dd77163c8c2903624b8f4b0ce
7
+ data.tar.gz: bd5856b089ff8195158ed352882dcb96525ba6fcc6c0d9ea24dc4be5db5197b23025fdd7f73a4eb9f9ebdc4a24af0a7cc1b31152eeae3dd9070e36ff560075b0
data/.fasterer.yml ADDED
@@ -0,0 +1,19 @@
1
+ speedups:
2
+ block_vs_symbol_to_proc: true
3
+ each_with_index_vs_while: false
4
+ fetch_with_argument_vs_block: true
5
+ for_loop_vs_each: true
6
+ getter_vs_attr_reader: true
7
+ gsub_vs_tr: true
8
+ hash_merge_bang_vs_hash_brackets: true
9
+ keys_each_vs_each_key: true
10
+ map_flatten_vs_flat_map: true
11
+ module_eval: true
12
+ proc_call_vs_yield: true
13
+ rescue_vs_respond_to: true
14
+ reverse_each_vs_reverse_each: true
15
+ select_first_vs_detect: true
16
+ select_last_vs_reverse_detect: true
17
+ setter_vs_attr_writer: true
18
+ shuffle_first_vs_sample: true
19
+ sort_vs_sort_by: true
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --backtrace
2
+ --color
3
+ --format progress
4
+ --order random
data/.rubocop.yml ADDED
@@ -0,0 +1,30 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rspec
4
+ AllCops:
5
+ TargetRubyVersion: 2.6
6
+ DisplayCopNames: true
7
+ DisplayStyleGuide: true
8
+ LineLength:
9
+ Max: 100
10
+ Layout/EmptyLinesAroundBlockBody:
11
+ Exclude:
12
+ - 'spec/**/**/*'
13
+ Layout/EmptyLinesAroundClassBody:
14
+ EnforcedStyle: empty_lines_except_namespace
15
+ Layout/EmptyLinesAroundModuleBody:
16
+ EnforcedStyle: empty_lines_except_namespace
17
+ Metrics/BlockLength:
18
+ Exclude:
19
+ - 'spec/**/**/*'
20
+ - '*.gemspec'
21
+ Naming/MemoizedInstanceVariableName:
22
+ Enabled: false
23
+ RSpec/ExampleLength:
24
+ Enabled: false
25
+ RSpec/MultipleExpectations:
26
+ Enabled: false
27
+ Style/Documentation:
28
+ Enabled: false
29
+ Style/ExpandPathArguments:
30
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,24 @@
1
+ sudo: false
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.5
6
+ - 2.6
7
+ - ruby-head
8
+ matrix:
9
+ fast_finish: true
10
+ allow_failures:
11
+ - rvm: ruby-head
12
+ before_install:
13
+ - gem update --system
14
+ - gem install bundler
15
+ install:
16
+ - bundle install --jobs=3 --retry=3
17
+ script:
18
+ - bundle exec rspec
19
+ - bundle exec rubocop
20
+ - bundle exec fasterer
21
+ notifications:
22
+ email: false
23
+ slack:
24
+ secure: pZ+VjpEsHwWJ7rxfcCX6UVr+8m5fxeJnRBCRJa4cR9QNYcCN4FUDd6HzZ7t/tSY+VgtuTqOKPN5szA0T5Ldvnh7gbNPnuX94KJN2nxBPcj5vzmJbt/V/XSn2DKZ86m3S/2bixbaNS0U2yOWTZMJIwGD2+nmz0uNS7885hMlm/Aa8U5JqYuktFYwo383iOvh+nUZLw8lPl59j4o+lo4LP+QHOJDj3gytPVo4TC3OSa5fqUa/HZbSSYWdoy/1pkAP0kqF3AbgEU/lPHbWGH7Kf1aJaWTJ3oGLrMzLWDjy1NCawFU65FfCMa6cF3AYCdNX5YFQjvonBblDXOfwopqthcXohZtQ1R8FACfHPG63eSCprQvb1TFqd+0nbzRZlXZNvikfLVtRvnUZGBmxTpbB8DfnPEnw6NA4s5A0iW4fJ+qRP3t7qUOGo3EzUsBTyRo+ZlZQ52h286E5vkbNmH9lcJDnCw2wicWHjQPMuya6FjD0gC7CO0W3+loi0S/ODU51MIKs3/0Iww9OUs27651fDu9wrUKq4AEoDM8ZT2cNsa+zKTzNH8JB+Ptz+ciZDZfEnrp0+Pe+ahiO/VS6NUBwGHlvqDVpiQXcFHfenAA5nK22F78qm5CGu+JjwuuDRcLwVfo0etmT3libSj3p9J6KBVvbndvGAsx+Juu0B+pitJyw=
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [1.0.0] - 2019-09-01
10
+ ### Added
11
+ - Initial project version
@@ -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 j.gomez@drexed.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 [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in lite-form.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,132 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lite-form (1.0.0)
5
+ activemodel
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionpack (6.0.0)
11
+ actionview (= 6.0.0)
12
+ activesupport (= 6.0.0)
13
+ rack (~> 2.0)
14
+ rack-test (>= 0.6.3)
15
+ rails-dom-testing (~> 2.0)
16
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
17
+ actionview (6.0.0)
18
+ activesupport (= 6.0.0)
19
+ builder (~> 3.1)
20
+ erubi (~> 1.4)
21
+ rails-dom-testing (~> 2.0)
22
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
23
+ activemodel (6.0.0)
24
+ activesupport (= 6.0.0)
25
+ activerecord (6.0.0)
26
+ activemodel (= 6.0.0)
27
+ activesupport (= 6.0.0)
28
+ activesupport (6.0.0)
29
+ concurrent-ruby (~> 1.0, >= 1.0.2)
30
+ i18n (>= 0.7, < 2)
31
+ minitest (~> 5.1)
32
+ tzinfo (~> 1.1)
33
+ zeitwerk (~> 2.1, >= 2.1.8)
34
+ ast (2.4.0)
35
+ builder (3.2.3)
36
+ colorize (0.8.1)
37
+ concurrent-ruby (1.1.5)
38
+ crass (1.0.4)
39
+ database_cleaner (1.7.0)
40
+ diff-lcs (1.3)
41
+ erubi (1.8.0)
42
+ fasterer (0.6.0)
43
+ colorize (~> 0.7)
44
+ ruby_parser (>= 3.13.0)
45
+ generator_spec (0.9.4)
46
+ activesupport (>= 3.0.0)
47
+ railties (>= 3.0.0)
48
+ i18n (1.6.0)
49
+ concurrent-ruby (~> 1.0)
50
+ jaro_winkler (1.5.3)
51
+ loofah (2.2.3)
52
+ crass (~> 1.0.2)
53
+ nokogiri (>= 1.5.9)
54
+ method_source (0.9.2)
55
+ mini_portile2 (2.4.0)
56
+ minitest (5.11.3)
57
+ nokogiri (1.10.4)
58
+ mini_portile2 (~> 2.4.0)
59
+ parallel (1.17.0)
60
+ parser (2.6.4.1)
61
+ ast (~> 2.4.0)
62
+ rack (2.0.7)
63
+ rack-test (1.1.0)
64
+ rack (>= 1.0, < 3)
65
+ rails-dom-testing (2.0.3)
66
+ activesupport (>= 4.2.0)
67
+ nokogiri (>= 1.6)
68
+ rails-html-sanitizer (1.2.0)
69
+ loofah (~> 2.2, >= 2.2.2)
70
+ railties (6.0.0)
71
+ actionpack (= 6.0.0)
72
+ activesupport (= 6.0.0)
73
+ method_source
74
+ rake (>= 0.8.7)
75
+ thor (>= 0.20.3, < 2.0)
76
+ rainbow (3.0.0)
77
+ rake (12.3.3)
78
+ rspec (3.8.0)
79
+ rspec-core (~> 3.8.0)
80
+ rspec-expectations (~> 3.8.0)
81
+ rspec-mocks (~> 3.8.0)
82
+ rspec-core (3.8.2)
83
+ rspec-support (~> 3.8.0)
84
+ rspec-expectations (3.8.4)
85
+ diff-lcs (>= 1.2.0, < 2.0)
86
+ rspec-support (~> 3.8.0)
87
+ rspec-mocks (3.8.1)
88
+ diff-lcs (>= 1.2.0, < 2.0)
89
+ rspec-support (~> 3.8.0)
90
+ rspec-support (3.8.2)
91
+ rubocop (0.74.0)
92
+ jaro_winkler (~> 1.5.1)
93
+ parallel (~> 1.10)
94
+ parser (>= 2.6)
95
+ rainbow (>= 2.2.2, < 4.0)
96
+ ruby-progressbar (~> 1.7)
97
+ unicode-display_width (>= 1.4.0, < 1.7)
98
+ rubocop-performance (1.4.1)
99
+ rubocop (>= 0.71.0)
100
+ rubocop-rspec (1.35.0)
101
+ rubocop (>= 0.60.0)
102
+ ruby-progressbar (1.10.1)
103
+ ruby_parser (3.13.1)
104
+ sexp_processor (~> 4.9)
105
+ sexp_processor (4.12.1)
106
+ sqlite3 (1.4.1)
107
+ thor (0.20.3)
108
+ thread_safe (0.3.6)
109
+ tzinfo (1.2.5)
110
+ thread_safe (~> 0.1)
111
+ unicode-display_width (1.6.0)
112
+ zeitwerk (2.1.10)
113
+
114
+ PLATFORMS
115
+ ruby
116
+
117
+ DEPENDENCIES
118
+ activerecord
119
+ bundler
120
+ database_cleaner
121
+ fasterer
122
+ generator_spec
123
+ lite-form!
124
+ rake
125
+ rspec
126
+ rubocop
127
+ rubocop-performance
128
+ rubocop-rspec
129
+ sqlite3
130
+
131
+ BUNDLED WITH
132
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Juan Gomez
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,157 @@
1
+ # Lite::Form
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/lite-form.svg)](http://badge.fury.io/rb/lite-form)
4
+ [![Build Status](https://travis-ci.org/drexed/lite-form.svg?branch=master)](https://travis-ci.org/drexed/lite-form)
5
+
6
+ Lite::Form is a library for using the form object pattern to separate business logic
7
+ from model from classes.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'lite-form'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install lite-form
24
+
25
+ ## Table of Contents
26
+
27
+ * [Setup](#setup)
28
+ * [Usage](#usage)
29
+ * [Forms](#forms)
30
+
31
+ ## Setup
32
+
33
+ `rails g lite:form:install` will generate the following file:
34
+ `../app/forms/application_form.rb`
35
+
36
+ ```ruby
37
+ class ApplicationForm < Lite::Form::Base
38
+ end
39
+ ```
40
+
41
+ Use `rails g form NAME` will generate the following file:
42
+ `../app/forms/[name]_form.rb`
43
+
44
+ You will then need to fill this class with the methods and actions you want to perform:
45
+
46
+ ```ruby
47
+ class UserForm < ApplicationForm
48
+
49
+ # Setup up your form attributes using active model attributes
50
+ # or PORO attr_*
51
+ attribute :name, :string
52
+ attribute :age, :integer, default: 18
53
+
54
+ attr_accessor :signature
55
+
56
+ # Setup your form validations like you would on model objects
57
+ validates :name, presence: true
58
+
59
+ # Access 4 predefined callbacks to trigger before, after, and
60
+ # around your actions. Available callbacks are `initialize`
61
+ # `create`, `save`, and `update`
62
+ before_create :prepend_signature!
63
+ after_update :append_signature!
64
+
65
+ private
66
+
67
+ # Action methods are required methods that get executed when
68
+ # you call an active model persistence such as `create`,
69
+ # `save`, and `update`
70
+ def create_action
71
+ # Propagation methods help you perform an action on an object.
72
+ # If successful is returns the result else it adds the object
73
+ # errors to the form object. Available propagation methods are
74
+ # `create_and_return!(object, params)`, `update_and_return!(object, params)`,
75
+ # `save_and_return!(object)`, and `destroy_and_return!(object)`
76
+ create_and_return!(User, attributes)
77
+ end
78
+
79
+ def update_action
80
+ user = User.find(attributes[:id])
81
+ update_and_return!(user, attributes.slice(:id))
82
+ end
83
+
84
+ def prepend_signature!
85
+ self.signature = "Prefix #{name}"
86
+ end
87
+
88
+ def append_signature!
89
+ self.signature = "#{signature} Suffix"
90
+ end
91
+
92
+ end
93
+ ```
94
+
95
+ ## Usage
96
+
97
+ To access the form you need to pass the object to the form class and thats it.
98
+ You can even decorate a collection of objects by passing the collection to `decorate`.
99
+
100
+ ```ruby
101
+ form = UserForm.new(params)
102
+ form.save #=> UserForm object
103
+
104
+ # - or -
105
+
106
+ UserForm.create(params) #=> UserForm object
107
+
108
+ # - or -
109
+
110
+ UserForm.perform(:update, params) do |result, success, failure|
111
+ success.call { redirect_to(user_path, notice: "User can be found at: #{result}") }
112
+ failure.call { redirect_to(root_path, notice: "User cannot be found at: #{result}") }
113
+ end
114
+ ```
115
+
116
+ ## Forms
117
+
118
+ The following mixins are included for you in the form base file so you can use it to setup
119
+ your form in any way, shape and form. The forms `model_name` will tried to be inferred from
120
+ the form object name, if not it will default back to its class name. There are also 4 most
121
+ common callbacks used with forms already setup to be used.
122
+
123
+ ``` ruby
124
+ # Default mixins that are included
125
+ extend ActiveModel::Callbacks
126
+ extend ActiveModel::Naming
127
+ extend ActiveModel::Translation
128
+
129
+ include ActiveModel::Model
130
+ include ActiveModel::Attributes
131
+ include ActiveModel::Dirty
132
+ include ActiveModel::Serialization
133
+
134
+ # Default callbacks that are defined
135
+ define_model_callbacks :initialize
136
+ define_model_callbacks :create
137
+ define_model_callbacks :save
138
+ define_model_callbacks :update
139
+ ```
140
+
141
+ ## Development
142
+
143
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
144
+
145
+ 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).
146
+
147
+ ## Contributing
148
+
149
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/lite-form. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
150
+
151
+ ## License
152
+
153
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
154
+
155
+ ## Code of Conduct
156
+
157
+ Everyone interacting in the Lite::Form project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/lite-form/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
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 'lite/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,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+
5
+ module Lite
6
+ module Form
7
+ class InstallGenerator < Rails::Generators::Base
8
+
9
+ source_root File.expand_path('../templates', __FILE__)
10
+
11
+ def copy_application_query_file
12
+ copy_file('install.rb', 'app/forms/application_form.rb')
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ApplicationForm < Lite::Form::Base
4
+ end
@@ -0,0 +1,9 @@
1
+ Description:
2
+ Generate a form file
3
+
4
+ Example:
5
+ rails generate form NAME
6
+
7
+ This will create:
8
+ - app/forms/[name]_form.rb
9
+ - spec/forms/[name]_form_spec.rb
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+
5
+ module Rails
6
+ class FormGenerator < Rails::Generators::NamedBase
7
+
8
+ source_root File.expand_path('../templates', __FILE__)
9
+ check_class_collision suffix: 'Form'
10
+
11
+ def copy_files
12
+ path = File.join('app', 'forms', class_path, "#{file_name}_form.rb")
13
+ empty_directory('app/forms')
14
+ template('form.rb.tt', path)
15
+ end
16
+
17
+ def copy_specs
18
+ path = File.join('spec', 'forms', class_path, "#{file_name}_form_spec.rb")
19
+ empty_directory('spec/forms')
20
+ template('spec.rb.tt', path)
21
+ end
22
+
23
+ private
24
+
25
+ def file_name
26
+ @_file_name ||= remove_possible_suffix(super)
27
+ end
28
+
29
+ def remove_possible_suffix(name)
30
+ name.sub(/_?form$/i, '')
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ <% module_namespacing do -%>
4
+ class <%= class_name %>Form < ApplicationForm
5
+ end
6
+ <% end -%>
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ RSpec.describe <%= class_name %>Form, type: :form do
6
+ pending "add some examples to (or delete) #{__FILE__}"
7
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Form
5
+ class Base
6
+
7
+ extend ActiveModel::Callbacks
8
+ extend ActiveModel::Naming
9
+ extend ActiveModel::Translation
10
+
11
+ include ActiveModel::Model
12
+ include ActiveModel::Attributes
13
+ include ActiveModel::Dirty
14
+ include ActiveModel::Serialization
15
+ include Lite::Form::Helpers::Errors
16
+ include Lite::Form::Helpers::Persistence
17
+ include Lite::Form::Helpers::Propagation
18
+
19
+ define_model_callbacks :initialize
20
+ define_model_callbacks :create
21
+ define_model_callbacks :save
22
+ define_model_callbacks :update
23
+
24
+ class << self
25
+
26
+ def model_name
27
+ klass = name.gsub('Form', '')
28
+ klass = Object.const_get(klass)
29
+ klass.model_name
30
+ rescue StandardError
31
+ super
32
+ end
33
+
34
+ end
35
+
36
+ attr_reader :result
37
+
38
+ def initialize(params = {})
39
+ run_callbacks(:initialize) { super(params) }
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Form
5
+ class NotImplementedError < StandardError; end
6
+ end
7
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Form
5
+ module Helpers
6
+ module Errors
7
+
8
+ module ClassMethods
9
+
10
+ def perform(action, params = {})
11
+ klass = %w[create update].include?(action.to_s) ? send(action, params) : send(action)
12
+
13
+ if klass.success?
14
+ yield(klass.result, Lite::Form::Success, Lite::Form::Failure)
15
+ else
16
+ yield(klass.result, Lite::Form::Failure, Lite::Form::Success)
17
+ end
18
+ end
19
+
20
+ end
21
+
22
+ class << self
23
+
24
+ def included(klass)
25
+ klass.extend(ClassMethods)
26
+ end
27
+
28
+ end
29
+
30
+ def errored?
31
+ !success?
32
+ end
33
+
34
+ def merge_errors!(klass, direction: :from)
35
+ case direction
36
+ when :from then errors.merge!(klass.errors)
37
+ when :to then klass.errors.merge!(errors)
38
+ end
39
+
40
+ nil
41
+ end
42
+
43
+ def merge_exception!(exception, key: :internal)
44
+ errors.add(key, "#{exception.class} - #{exception.message}")
45
+
46
+ nil
47
+ end
48
+
49
+ def success?
50
+ errors.empty?
51
+ end
52
+
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Form
5
+ module Helpers
6
+ module Persistence
7
+
8
+ module ClassMethods
9
+
10
+ %i[create save update].each do |method_name|
11
+ define_method(method_name) do |params = {}|
12
+ klass = new(params)
13
+ klass.send(method_name)
14
+ klass
15
+ end
16
+ end
17
+
18
+ end
19
+
20
+ class << self
21
+
22
+ def included(klass)
23
+ klass.extend(ClassMethods)
24
+ end
25
+
26
+ end
27
+
28
+ def persisted?
29
+ false
30
+ end
31
+
32
+ def create
33
+ raise Lite::Form::NotImplementedError unless defined?(create_action)
34
+ return unless valid?
35
+
36
+ run_callbacks(:create) { @result = create_action }
37
+ end
38
+
39
+ def save
40
+ raise Lite::Form::NotImplementedError unless defined?(save_action)
41
+ return unless valid?
42
+
43
+ run_callbacks(:save) { @result = save_action }
44
+ end
45
+
46
+ def update
47
+ raise Lite::Form::NotImplementedError unless defined?(update_action)
48
+ return unless valid?
49
+
50
+ run_callbacks(:update) { @result = update_action }
51
+ end
52
+
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Form
5
+ module Helpers
6
+ module Propagation
7
+
8
+ private
9
+
10
+ %i[archive destroy save].each do |action|
11
+ define_method("#{action}_and_return!") do |klass|
12
+ errors.merge!(klass.errors) unless klass.send(action)
13
+ klass
14
+ end
15
+ end
16
+
17
+ def create_and_return!(klass, params)
18
+ klass = klass.create(params)
19
+ errors.merge!(klass.errors) unless klass
20
+ klass
21
+ end
22
+
23
+ def update_and_return!(klass, params)
24
+ errors.merge!(klass.errors) unless klass.update(params)
25
+ klass
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Form
5
+
6
+ class Success
7
+
8
+ class << self
9
+
10
+ def call
11
+ yield
12
+ end
13
+
14
+ end
15
+
16
+ end
17
+
18
+ class Failure
19
+
20
+ class << self
21
+
22
+ def call
23
+ # Do nothing
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Form
5
+
6
+ VERSION ||= '1.0.0'
7
+
8
+ end
9
+ end
data/lib/lite/form.rb ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_model'
4
+ require 'lite/form/version'
5
+
6
+ %w[propagation persistence errors].each do |name|
7
+ require "lite/form/helpers/#{name}"
8
+ end
9
+
10
+ %w[exceptions states base].each do |name|
11
+ require "lite/form/#{name}"
12
+ end
13
+
14
+ require 'generators/lite/form/install_generator'
15
+ require 'generators/rails/form_generator'
data/lite-form.gemspec ADDED
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'lite/form/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'lite-form'
9
+ spec.version = Lite::Form::VERSION
10
+ spec.authors = ['Juan Gomez']
11
+ spec.email = %w[j.gomez@drexed.com]
12
+
13
+ spec.summary = 'Ruby Form based framework (aka form objects)'
14
+ spec.homepage = 'http://drexed.github.io/lite-form'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata.merge(
21
+ 'allowed_push_host' => 'https://rubygems.org',
22
+ 'changelog_uri' => 'https://github.com/drexed/lite-form/blob/master/CHANGELOG.md',
23
+ 'homepage_uri' => spec.homepage,
24
+ 'source_code_uri' => 'https://github.com/drexed/lite-form'
25
+ )
26
+ else
27
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
28
+ 'public gem pushes.'
29
+ end
30
+
31
+ # Specify which files should be added to the gem when it is released.
32
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
33
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
34
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
35
+ end
36
+ spec.bindir = 'exe'
37
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
38
+ spec.require_paths = %w[lib]
39
+
40
+ spec.add_runtime_dependency 'activemodel'
41
+
42
+ spec.add_development_dependency 'activerecord'
43
+ spec.add_development_dependency 'bundler'
44
+ spec.add_development_dependency 'database_cleaner'
45
+ spec.add_development_dependency 'fasterer'
46
+ spec.add_development_dependency 'generator_spec'
47
+ spec.add_development_dependency 'rake'
48
+ spec.add_development_dependency 'rspec'
49
+ spec.add_development_dependency 'rubocop'
50
+ spec.add_development_dependency 'rubocop-performance'
51
+ spec.add_development_dependency 'rubocop-rspec'
52
+ spec.add_development_dependency 'sqlite3'
53
+ end
metadata ADDED
@@ -0,0 +1,240 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lite-form
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Juan Gomez
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-09-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: bundler
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
+ - !ruby/object:Gem::Dependency
56
+ name: database_cleaner
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: fasterer
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: generator_spec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop-performance
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rubocop-rspec
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: sqlite3
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ description:
182
+ email:
183
+ - j.gomez@drexed.com
184
+ executables: []
185
+ extensions: []
186
+ extra_rdoc_files: []
187
+ files:
188
+ - ".fasterer.yml"
189
+ - ".gitignore"
190
+ - ".rspec"
191
+ - ".rubocop.yml"
192
+ - ".travis.yml"
193
+ - CHANGELOG.md
194
+ - CODE_OF_CONDUCT.md
195
+ - Gemfile
196
+ - Gemfile.lock
197
+ - LICENSE.txt
198
+ - README.md
199
+ - Rakefile
200
+ - bin/console
201
+ - bin/setup
202
+ - lib/generators/lite/form/install_generator.rb
203
+ - lib/generators/lite/form/templates/install.rb
204
+ - lib/generators/rails/USAGE
205
+ - lib/generators/rails/form_generator.rb
206
+ - lib/generators/rails/templates/form.rb.tt
207
+ - lib/generators/rails/templates/spec.rb.tt
208
+ - lib/lite/form.rb
209
+ - lib/lite/form/base.rb
210
+ - lib/lite/form/exceptions.rb
211
+ - lib/lite/form/helpers/errors.rb
212
+ - lib/lite/form/helpers/persistence.rb
213
+ - lib/lite/form/helpers/propagation.rb
214
+ - lib/lite/form/states.rb
215
+ - lib/lite/form/version.rb
216
+ - lite-form.gemspec
217
+ homepage: http://drexed.github.io/lite-form
218
+ licenses:
219
+ - MIT
220
+ metadata: {}
221
+ post_install_message:
222
+ rdoc_options: []
223
+ require_paths:
224
+ - lib
225
+ required_ruby_version: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - ">="
228
+ - !ruby/object:Gem::Version
229
+ version: '0'
230
+ required_rubygems_version: !ruby/object:Gem::Requirement
231
+ requirements:
232
+ - - ">="
233
+ - !ruby/object:Gem::Version
234
+ version: '0'
235
+ requirements: []
236
+ rubygems_version: 3.0.4
237
+ signing_key:
238
+ specification_version: 4
239
+ summary: Ruby Form based framework (aka form objects)
240
+ test_files: []