dejavu 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module Dejavu
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/dejavu.rb CHANGED
@@ -39,14 +39,21 @@ module Dejavu
39
39
 
40
40
  module ControllerMethods
41
41
  def save_for_dejavu(obj, opts = {})
42
- attrs = obj.attributes
42
+ attrs = if opts[:only] && opts[:only].is_a?(Array)
43
+ obj.attributes.slice(*opts[:only].map(&:to_s))
44
+ else
45
+ obj.attributes
46
+ end
43
47
 
44
- missing_keys = obj.errors.keys.map(&:to_sym).select{|x| obj.respond_to?(x)} - attrs.keys.map(&:to_sym)
48
+ missing_keys = []
45
49
 
46
50
  if keys = opts[:nested]
47
- keys = [keys] unless keys.is_a? Array
51
+ keys = [keys].flatten
48
52
  keys.each { |key| attrs = save_nested_for_dejavu(obj, key, attrs) }
49
- missing_keys -= keys.map(&:to_sym)
53
+ end
54
+
55
+ if virtual = opts[:virtual]
56
+ missing_keys += [virtual].flatten
50
57
  end
51
58
 
52
59
  missing_keys.each do |key|
@@ -71,15 +71,45 @@ describe "Dejavu" do
71
71
  fill_in "Name", :with => "Mug"
72
72
  fill_in "Code", :with => "PT"
73
73
  fill_in "Virtual", :with => "ou"
74
- click_button "Create Product"
75
74
  end
76
75
 
77
- it "should be prefilled" do
76
+ it "should be prefilled on errors" do
77
+ click_button "Create Product"
78
78
  field_should_have "product_virtual", "ou"
79
79
  end
80
80
 
81
+ it "should be prefilled if the virtual attribute is ok but there is an error in the form" do
82
+ fill_in "Virtual", :with => "correct"
83
+ click_button "Create Product"
84
+ field_should_have "product_virtual", "correct"
85
+ end
86
+
81
87
  it "should show existing errors" do
88
+ click_button "Create Product"
82
89
  page.should have_content("Virtual is too short")
83
90
  end
84
91
  end
92
+
93
+ describe "only option" do
94
+ it "should not remember errors on code" do
95
+ visit new_only_name_products_path
96
+
97
+ fill_in "Create only: name", :with => "Mug"
98
+ fill_in "Create only: code", :with => "PT"
99
+ click_button "Create only name"
100
+
101
+ # Blank error is still there because we don't reset the PT
102
+ page.should have_content("Code can't be blank")
103
+ end
104
+
105
+ it "should remember errors on name" do
106
+ visit new_only_name_products_path
107
+
108
+ fill_in "Create only: name", :with => "pt"
109
+ fill_in "Create only: code", :with => "PTujarsa"
110
+ click_button "Create only name"
111
+
112
+ page.should have_no_content("Name can't be blank")
113
+ end
114
+ end
85
115
  end
@@ -34,6 +34,15 @@ class ProductsController < ApplicationController
34
34
  end
35
35
  end
36
36
 
37
+ def new_only_name
38
+ @product = Product.new
39
+
40
+ respond_to do |format|
41
+ format.html # new.html.erb
42
+ format.json { render json: @product }
43
+ end
44
+ end
45
+
37
46
  # GET /products/1/edit
38
47
  def edit
39
48
  @product = Product.find(params[:id])
@@ -49,13 +58,28 @@ class ProductsController < ApplicationController
49
58
  format.html { redirect_to @product, notice: 'Product was successfully created.' }
50
59
  format.json { render json: @product, status: :created, location: @product }
51
60
  else
52
- save_for_dejavu @product, :nested => [:category, :colors]
61
+ save_for_dejavu @product, :nested => [:category, :colors], :virtual => :virtual
53
62
  format.html { redirect_to new_product_url }
54
63
  format.json { render json: @product.errors, status: :unprocessable_entity }
55
64
  end
56
65
  end
57
66
  end
58
67
 
68
+ def create_only_name
69
+ @product = Product.new(params[:product])
70
+
71
+ respond_to do |format|
72
+ if @product.save
73
+ format.html { redirect_to :back, notice: 'Product was successfully created.' }
74
+ format.json { render json: @product, status: :created, location: @product }
75
+ else
76
+ save_for_dejavu @product, :only => [:name]
77
+ format.html { redirect_to :back }
78
+ format.json { render json: @product.errors, status: :unprocessable_entity }
79
+ end
80
+ end
81
+ end
82
+
59
83
  # PUT /products/1
60
84
  # PUT /products/1.json
61
85
  def update
@@ -39,3 +39,4 @@
39
39
  <%= f.submit %>
40
40
  </div>
41
41
  <% end %>
42
+
@@ -0,0 +1,26 @@
1
+ <%= form_for(get_dejavu_for(@product), :url => create_only_name_products_path) do |f| %>
2
+ <% if @product.errors.any? %>
3
+ <div id="error_explanation">
4
+ <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>
5
+
6
+ <ul>
7
+ <% @product.errors.full_messages.each do |msg| %>
8
+ <li><%= msg %></li>
9
+ <% end %>
10
+ </ul>
11
+ </div>
12
+ <% end %>
13
+
14
+ <div class="field">
15
+ <%= f.label :name, "Create only: name" %><br />
16
+ <%= f.text_field :name %>
17
+ </div>
18
+ <div class="field">
19
+ <%= f.label :code, "Create only: code" %><br />
20
+ <%= f.text_field :code %>
21
+ </div>
22
+ <div class="actions">
23
+ <%= f.submit "Create only name" %>
24
+ </div>
25
+ <% end %>
26
+
@@ -1,5 +1,8 @@
1
1
  TestApp::Application.routes.draw do
2
- resources :products
2
+ resources :products do
3
+ post :create_only_name, :on => :collection
4
+ get :new_only_name, :on => :collection
5
+ end
3
6
 
4
7
  root :to => "products#index"
5
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dejavu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-16 00:00:00.000000000 Z
12
+ date: 2012-08-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &79605650 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *79605650
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: rspec-rails
27
- requirement: &79605170 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '2.7'
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *79605170
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '2.7'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: capybara
38
- requirement: &79604600 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,7 +53,12 @@ dependencies:
43
53
  version: 1.1.1
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *79604600
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.1.1
47
62
  description: Remember your object after a redirect
48
63
  email:
49
64
  - roger@itnig.net
@@ -87,6 +102,7 @@ files:
87
102
  - test_app/app/views/products/edit.html.erb
88
103
  - test_app/app/views/products/index.html.erb
89
104
  - test_app/app/views/products/new.html.erb
105
+ - test_app/app/views/products/new_only_name.html.erb
90
106
  - test_app/app/views/products/show.html.erb
91
107
  - test_app/config.ru
92
108
  - test_app/config/application.rb
@@ -148,7 +164,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
148
164
  version: '0'
149
165
  segments:
150
166
  - 0
151
- hash: 730709121
167
+ hash: 995488367
152
168
  required_rubygems_version: !ruby/object:Gem::Requirement
153
169
  none: false
154
170
  requirements:
@@ -157,10 +173,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
157
173
  version: '0'
158
174
  segments:
159
175
  - 0
160
- hash: 730709121
176
+ hash: 995488367
161
177
  requirements: []
162
178
  rubyforge_project:
163
- rubygems_version: 1.8.15
179
+ rubygems_version: 1.8.24
164
180
  signing_key:
165
181
  specification_version: 3
166
182
  summary: Remember your object after a redirect