aform 0.0.6 → 0.0.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b539a689687d10ce372376c984749ac30cbe65cc
4
- data.tar.gz: addef8dbcb1055f19ce403ae320c4e37532d6ee3
3
+ metadata.gz: d9df2b57aeb8971ff9e39d745dcd5eff638dda62
4
+ data.tar.gz: e571e37db8431d9058396d5ffa2400ead9ccb501
5
5
  SHA512:
6
- metadata.gz: 5b6c8024ce914f97a27845459d2003c18d949974a86703a0e75949d38b1588aeb70da76533464eda580563396bea4942d36e9c6d7d37a7e679ea94a01d4e7f4b
7
- data.tar.gz: 95fb5e0da238837daf95f78ca910f098b984d700b46d049873571ff20ca2f4214ca98a077fbdd90640a75a3e6d8a7f3df4edfd1273af0d64b1de770dc19b1e83
6
+ metadata.gz: 6179f8685ab941adbe1bae32685857787978c7931cb47698968aee0d51e82915132526205afe2ec28e3eff30d276e35f7adab6fa4ead45811f184f74b80a6749
7
+ data.tar.gz: 07d14ba691df39bc0f35f0ad77f6a2d8caddfebe36146f5ea9f8400cf8bb0c9da9970095d2f81db77f78f714cdad1b649e159d932e2aabe27169209f66c9b60a
data/README.md CHANGED
@@ -1,15 +1,14 @@
1
- # Aform
2
- [![Build Status](https://travis-ci.org/antonversal/aform.svg?branch=master)](https://travis-ci.org/antonversal/aform)
1
+ # Aform [![Build Status](https://travis-ci.org/antonversal/aform.svg?branch=master)](https://travis-ci.org/antonversal/aform) [![Code Quality](https://codeclimate.com/github/antonversal/aform.png)](https://codeclimate.com/github/antonversal/aform)
3
2
 
4
- [![Code Quality](https://codeclimate.com/github/antonversal/aform.png)](https://codeclimate.com/github/antonversal/aform)
3
+ Aform lets you define Form Object with validatins and ability save difficult `jsons` or `form params` to models. You can create Form object with nested forms for storing `has_many` associations with parent model.
5
4
 
6
- TODO: Write a gem description
5
+ It was developed for [rails-api](https://github.com/rails-api/rails-api) and ActiveRecord.
7
6
 
8
7
  ## Installation
9
8
 
10
9
  Add this line to your application's Gemfile:
11
10
 
12
- gem 'aform'
11
+ gem 'aform', '~>0.0.7'
13
12
 
14
13
  And then execute:
15
14
 
@@ -20,12 +19,103 @@ Or install it yourself as:
20
19
  $ gem install aform
21
20
 
22
21
  ## Usage
22
+ ### Define Form
23
23
 
24
- TODO: Write usage instructions here
24
+ Definning form is pretty simple, for instance you have posts wich have comments and comments have likes:
25
+
26
+ ```ruby
27
+ class PostForm < Aform::Form
28
+ param :title, :author
29
+ validates_presence_of :title, :author
30
+
31
+ has_many :comments do
32
+ param :message, :author
33
+ validates_presence_of :message, :author
34
+
35
+ has_many :likes do
36
+ param :author
37
+ validates_presence_of :author
38
+ end
39
+ end
40
+ end
41
+ ```
42
+ Method `param` is used for adding params wich will be used for saving models. You can add any validation you need for each param.
43
+
44
+ ### Setup controller
45
+ ``` ruby
46
+ class PostsController < ApplicationController
47
+
48
+ def create
49
+ form = PostForm.new(Post.new, params[:post])
50
+ if form.save
51
+ render json: form.model, status: :created
52
+ else
53
+ render json: {errors: form.errors}, status: :unprocessable_entity
54
+ end
55
+ end
56
+
57
+ def update
58
+ post = Post.find(params[:id])
59
+ form = PostForm.new(post, params[:post])
60
+ if form.save
61
+ render json: form.model
62
+ else
63
+ render json: {errors: form.errors}, status: :unprocessable_entity
64
+ end
65
+ end
66
+
67
+ end
68
+ ```
69
+
70
+ ### Delete nested records
71
+
72
+ For deleting nested records you should set `_destroy` key to `true` in nested params:
73
+ ```ruby
74
+ post = {
75
+ title: "Very Cool Post",
76
+ author: "John Doe",
77
+ comments: [ {id: comment.id, _destroy: true}]
78
+ }
79
+ ```
80
+
81
+ ### Params
82
+ Before looking up the param in a given hash, a form object will check for the presence of a method with the name of the param:
83
+ ```ruby
84
+ post = {
85
+ title: "Cool Post",
86
+ first_author: "John Doe",
87
+ second_author: "Mr. Author"
88
+ }
89
+ ```
90
+ Form Object:
91
+ ``` ruby
92
+ class OtherPostForm < Aform::Form
93
+ param :title, :author
94
+
95
+ def author(attributes)
96
+ "#{attributes[:first_author]} and #{attributes[:second_author]}"
97
+ end
98
+ end
99
+ ```
100
+
101
+ When you need just save method with other name you can use `:model_field` option:
102
+
103
+ ``` ruby
104
+ param :first_author, model_field: :author
105
+ ```
106
+
107
+ ### Validations
108
+ Aform curruntly support standart ActiveModel validations like:
109
+ ```ruby
110
+ validates_presence_of :title
111
+ validates :count, presence: true, inclusion: [1..100]
112
+ ```
113
+ And ActiveRecord uniqueness validation `validates_uniqueness_of` or `validates :title, uniqueness: true`
114
+ But it doesn't support yet validation with block and a symbol pointing to a method, supporting will be added in short future.
25
115
 
26
116
  ## Contributing
27
117
 
28
- 1. Fork it ( https://github.com/[my-github-username]/aform/fork )
118
+ 1. Fork it ( https://github.com/antonversal/aform )
29
119
  2. Create your feature branch (`git checkout -b my-new-feature`)
30
120
  3. Commit your changes (`git commit -am 'Add some feature'`)
31
121
  4. Push to the branch (`git push origin my-new-feature`)
data/lib/aform/builder.rb CHANGED
@@ -12,7 +12,11 @@ class Aform::Builder
12
12
  self.params = params
13
13
 
14
14
  validations.each do |v|
15
- send(v[:method], *v[:options])
15
+ if v[:block]
16
+ send(v[:method], v[:block])
17
+ else
18
+ send(v[:method], *v[:options])
19
+ end
16
20
  end if validations
17
21
 
18
22
  params.each do |p|
data/lib/aform/model.rb CHANGED
@@ -52,7 +52,7 @@ class Aform::Model
52
52
  if @form.respond_to?(p[:field])
53
53
  memo.merge(field_name => @form.public_send(p[:field], attrs))
54
54
  else
55
- if attrs[p[:field]]
55
+ if !attrs[p[:field]].nil?
56
56
  memo.merge(field_name => attrs[p[:field]])
57
57
  else
58
58
  memo
data/lib/aform/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Aform
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
data/test/model_test.rb CHANGED
@@ -48,8 +48,15 @@ describe Aform::Model do
48
48
  [{method: :validate, block: ->{errors.add(:base, "must be foo")}}]
49
49
  end
50
50
 
51
+ let(:model) {subject.new(mock_ar_model, mock_form, {})}
52
+
51
53
  it "is not valid" do
52
- skip("not implemented")
54
+ model.wont_be :valid?
55
+ end
56
+
57
+ it "adds errors" do
58
+ model.valid?
59
+ model.errors.messages.must_equal(base: ["must be foo"])
53
60
  end
54
61
  end
55
62
  end
@@ -82,6 +89,15 @@ describe Aform::Model do
82
89
  end
83
90
  end
84
91
 
92
+ context "when field with boolean false value" do
93
+ let(:form_model) { subject.new(model, mock_form, name: "name", count: false)}
94
+
95
+ it "calls assigns attributes" do
96
+ form_model.save
97
+ model.attributes.must_equal(name: "name", count: false)
98
+ end
99
+ end
100
+
85
101
  context "when fields with model_field option" do
86
102
  let(:fields){ [{field: :name}, {field: :count, options: {model_field: :size}}] }
87
103
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aform
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Versal
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-24 00:00:00.000000000 Z
11
+ date: 2014-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport