Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add save! to raise exception if things go wrong #19

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
732f9c6
Add save! to raise exception if things go wrong
Nov 20, 2014
2739020
revert whitespace change for easier merging
willbryant Jul 6, 2015
36c6653
merged rails/master
willbryant Jul 6, 2015
f17dd6b
Remove Gemfile.lock
Jul 5, 2016
209dfb3
Fix deprecation warnings
Jul 5, 2016
ccdfa22
Fix method signature for SimpleForm
Jul 5, 2016
a102adc
Fix attributes order in SimpleForm forms
Jul 5, 2016
939862e
Update README.md
Jul 5, 2016
faac7ec
Update License file
Jul 5, 2016
e4777dd
Update .gitignore
Jul 5, 2016
e893090
Fix deprecation warnings
Jul 5, 2016
1590bf4
Oopss.. typo
Jul 5, 2016
7a9dfb5
Remove empty lines
Jul 5, 2016
f39ef86
Setup tests with Appraisals
Jul 5, 2016
a7bb4c0
Fix warning
Jul 5, 2016
3238154
Add code coverage
Jul 5, 2016
72d9cb0
Update README.md
Jul 5, 2016
56542ca
Update README.md
Jul 5, 2016
2acfb03
Update README.md
Jul 5, 2016
30fe7b4
Fix tests with Rails 5 (part1)
Jul 5, 2016
6275c7b
Fix warning
Jul 5, 2016
b6f4535
Fix tests for Rails5
Jul 5, 2016
da36952
Revert "Fix warning"
Jul 5, 2016
b69e05a
Reduce Rails versions range in gemspec
Jul 5, 2016
6ec2f5f
Becareful of module namespace
Jul 5, 2016
612d09d
Add comment
Jul 10, 2016
c1cf72e
ActionForm should not depend on SimpleForm (only for tests)
Jul 10, 2016
c4382c0
Fix CodeClimate error
Jan 18, 2017
be5c214
Test with latest Ruby version
Jan 18, 2017
1ce4cdd
Fix CodeClimate reporter
n-rodriguez Mar 24, 2017
52afb96
Test with latest Ruby version
n-rodriguez Mar 24, 2017
b6376ac
Test with latest Ruby versions
n-rodriguez Apr 2, 2017
1860a35
Test with maintained versions of Rails
Apr 28, 2017
3fd79a7
Merge pull request #1 from jbox-web/master
willbryant May 3, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
.bundle/
log/*.log
pkg/
test/dummy/db/*.sqlite3
test/dummy/db/*.sqlite3-journal
test/dummy/log/*.log
test/dummy/tmp/
test/dummy/.sass-cache
tmp
# Ignore bundler config.
/.bundle

# Ignore Gemfile.lock
/Gemfile.lock
/gemfiles/*.gemfile.lock

# Ignore test files
/coverage

# Ignore dummy app files
/test/dummy/db/*.sqlite3
/test/dummy/db/*.sqlite3-journal
/test/dummy/log/*.log
/test/dummy/tmp/

# Temp files
/tmp
13 changes: 11 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
language: ruby
sudo: false
cache: bundler
rvm:
- 2.1
- 2.2
- 2.4.1
- 2.3.4
- 2.2.7
gemfile:
- gemfiles/rails_4.2.8.gemfile
- gemfiles/rails_5.0.2.gemfile
- gemfiles/rails_5.1.0.gemfile
script:
- cd test/dummy/
- RAILS_ENV=test bundle exec rake db:migrate
- cd ../..
- bundle exec rake
after_success:
- bundle exec codeclimate-test-reporter
12 changes: 12 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
RAILS_VERSIONS = %w(
4.2.8
5.0.2
5.1.0
)

RAILS_VERSIONS.each do |version|
appraise "rails_#{version}" do
gem 'rails', version
gem 'rails-controller-testing' if version != '4.2.8'
end
end
6 changes: 5 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ source 'https://rubygems.org'
gemspec

gem 'jquery-rails'
gem 'simple_form'

group :test do
gem 'simple_form'
gem 'codeclimate-test-reporter', require: nil
end
96 changes: 0 additions & 96 deletions Gemfile.lock

This file was deleted.

19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
20 changes: 0 additions & 20 deletions MIT-LICENSE

This file was deleted.

13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Action Form

# DISCLAIMER: This project is an experiment and should not be used in applications.

[![Build Status](https://api.travis-ci.org/railsgsoc/actionform.svg?branch=master)](https://travis-ci.org/railsgsoc/actionform)
## Action Form

[![GitHub license](https://img.shields.io/github/license/jbox-web/actionform.svg)](https://github.com/jbox-web/actionform/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/jbox-web/actionform.svg)](https://github.com/jbox-web/actionform/releases/latest)
[![Build Status](https://travis-ci.org/jbox-web/actionform.svg?branch=master)](https://travis-ci.org/jbox-web/actionform)
[![Code Climate](https://codeclimate.com/github/jbox-web/actionform/badges/gpa.svg)](https://codeclimate.com/github/jbox-web/actionform)
[![Test Coverage](https://codeclimate.com/github/jbox-web/actionform/badges/coverage.svg)](https://codeclimate.com/github/jbox-web/actionform/coverage)
[![Dependency Status](https://gemnasium.com/badges/github.com/jbox-web/actionform.svg)](https://gemnasium.com/github.com/jbox-web/actionform)

Set your models free from the `accepts_nested_attributes_for` helper. Action Form provides an object-oriented approach to represent your forms by building a form object, rather than relying on Active Record internals for doing this. Form objects provide an API to describe the models involved in the form, their attributes and validations. A form object deals with create/update actions of nested objects in a more seamless way.

Expand Down
9 changes: 5 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#!/usr/bin/env rake

require 'rubygems'

begin
require 'bundler/setup'
rescue LoadError
Expand All @@ -6,6 +10,7 @@ end

require 'rdoc/task'

desc 'Generate documentation.'
RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'ActionForm'
Expand All @@ -14,9 +19,6 @@ RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_files.include('lib/**/*.rb')
end




Bundler::GemHelper.install_tasks

require 'rake/testtask'
Expand All @@ -28,5 +30,4 @@ Rake::TestTask.new(:test) do |t|
t.verbose = false
end


task default: :test
19 changes: 12 additions & 7 deletions actionform.gemspec
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path('../lib', __FILE__)

require 'action_form/version'

Gem::Specification.new do |s|
s.name = 'actionform'
s.version = ActionForm::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ['Petros Markou']
s.email = ['[email protected]']
s.homepage = 'https://github.com/rails/actionform'
s.homepage = 'https://github.com/jbox-web/actionform'
s.summary = 'Create nested forms with ease.'
s.description = 'An alternative layer to accepts_nested_attributes_for by using Form Models.'
s.license = 'MIT'

s.files = Dir['{app,config,db,lib}/**/*', 'MIT-LICENSE', 'Rakefile', 'README.md']
s.test_files = Dir['test/**/*']

s.add_dependency 'rails', '~> 4.1'
s.add_dependency 'rails', '>= 4.2'

s.add_development_dependency 'sqlite3'
s.add_development_dependency 'rake', '~> 10.3.2'
s.add_development_dependency 'rake'
s.add_development_dependency 'simplecov'
s.add_development_dependency 'appraisal'

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ['lib']
end
13 changes: 13 additions & 0 deletions gemfiles/rails_4.2.8.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "jquery-rails"
gem "rails", "4.2.8"

group :test do
gem "simple_form"
gem "codeclimate-test-reporter", require: nil
end

gemspec path: "../"
14 changes: 14 additions & 0 deletions gemfiles/rails_5.0.2.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "jquery-rails"
gem "rails", "5.0.2"
gem "rails-controller-testing"

group :test do
gem "simple_form"
gem "codeclimate-test-reporter", require: nil
end

gemspec path: "../"
14 changes: 14 additions & 0 deletions gemfiles/rails_5.1.0.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "jquery-rails"
gem "rails", "5.1.0"
gem "rails-controller-testing"

group :test do
gem "simple_form"
gem "codeclimate-test-reporter", require: nil
end

gemspec path: "../"
4 changes: 4 additions & 0 deletions lib/action_form/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def get_model(assoc_name)
form.get_model(assoc_name)
end

def save!
save or raise ActiveRecord::RecordInvalid.new(self)
end

def save
if valid?
run_callbacks :save do
Expand Down
2 changes: 1 addition & 1 deletion lib/action_form/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize(assoc_name, parent, proc, model=nil)
@forms = []
@proc = proc
enable_autosave
instance_eval &proc
instance_eval(&proc)
end

def class
Expand Down
8 changes: 6 additions & 2 deletions lib/action_form/form_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def update_models

def submit(params)
params.each do |key, value|
value = value.to_h if value.is_a?(ActionController::Parameters) && rails_5?
if parent.persisted?
create_or_update_record(value)
else
Expand Down Expand Up @@ -120,7 +121,7 @@ def assign_forms
end

def dynamic_key?(i)
i > forms.size
i >= forms.size
end

def aggregate_form_errors
Expand Down Expand Up @@ -171,6 +172,9 @@ def create_form
forms << new_form
new_form
end
end

def rails_5?
Rails.version[0] == '5'
end
end
end
Loading