dejavu 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
4
+
5
+ env: "RAILS_ENV=test DISPLAY=:99.0"
6
+
7
+ before_script:
8
+ - "sh -c 'cd test_app && bundle && bundle exec rake db:drop db:migrate'"
9
+ - "sh -e /etc/init.d/xvfb start"
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  Dejavu
2
2
  ======
3
+ [![Build Status](https://secure.travis-ci.org/rogercampos/dejavu.png)](http://travis-ci.org/rogercampos/dejavu)
3
4
 
4
5
  Dejavu is a very small piece of software which lets you remember your object
5
6
  state after a failed POST action using redirect. A typical Rails controller
@@ -68,14 +69,15 @@ Then, in the view load the object in the form usign the `get_dejavu_for`:
68
69
  Nested Attributes
69
70
  -----------------
70
71
 
71
- You can also use dejavu to recreate a one-to-one associated model in your
72
- form. To do so you must tell dejavu which association to save in the
73
- controller:
72
+ You can also use dejavu to recreate any associated model in your form. This associated model can come from a one-to-one, many-to-one or one-to-many association. To do so you must tell dejavu which association to save in the controller:
74
73
 
75
74
  save_for_dejavu @product, :nested => :prices
76
75
 
77
- This will assume you have a `Product` class which has a `:has_one` association
78
- to a `Price` class.
76
+ You can even use dejavu for multiple associations:
77
+
78
+ save_for_dejavu @product, :nested => [:category, :colors]
79
+
80
+ This will assume you have a `Product` class which has a simple association (with an element of the `Price` class) or a multiple association (with elements of the `Price` class).
79
81
 
80
82
 
81
83
  Excluding some errors
data/Rakefile CHANGED
@@ -1,2 +1,8 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core'
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
data/lib/dejavu.rb CHANGED
@@ -40,11 +40,26 @@ module Dejavu
40
40
  module ControllerMethods
41
41
  def save_for_dejavu(obj, opts = {})
42
42
  attrs = obj.attributes
43
- if opts[:nested]
44
- attrs["#{opts[:nested]}_attributes"] = obj.send(opts[:nested]).attributes
43
+ if keys = opts[:nested]
44
+ keys = [keys] unless keys.is_a? Array
45
+ keys.each { |key| attrs = save_nested_for_dejavu(obj, key, attrs) }
45
46
  end
46
47
  flash[:"saved_#{obj.class.model_name.underscore}_for_redisplay"] = attrs
47
48
  end
49
+
50
+ private
51
+
52
+ def save_nested_for_dejavu(obj, key, attrs)
53
+ value = obj.send(key)
54
+
55
+ attrs["#{key}_attributes"] = if value.is_a? Array
56
+ value.map(&:attributes)
57
+ else
58
+ value.attributes
59
+ end
60
+
61
+ attrs
62
+ end
48
63
  end
49
64
  end
50
65
 
@@ -1,3 +1,3 @@
1
1
  module Dejavu
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -6,6 +6,8 @@ describe "Dejavu" do
6
6
  visit new_product_path
7
7
  fill_in "Name", :with => "Mug"
8
8
  fill_in "Code", :with => "PT"
9
+ fill_in "product_category_attributes_name", :with => "Mugs"
10
+ fill_in "product_colors_attributes_0_name", :with => "Blue"
9
11
  click_button "Create Product"
10
12
  end
11
13
 
@@ -21,11 +23,20 @@ describe "Dejavu" do
21
23
  field_should_have "product_name", "Mug"
22
24
  field_should_have "product_code", "PT"
23
25
  end
26
+
27
+ it "should have the many-to-one association prefilled with the previously entered values" do
28
+ field_should_have "product_category_attributes_name", "Mugs"
29
+ end
30
+
31
+ it "should have the one-to-many association prefilled with the previously entered values" do
32
+ field_should_have "product_colors_attributes_1_name", "Blue"
33
+ end
24
34
  end
25
35
 
26
36
  describe "update" do
27
37
  before do
28
- @p = Product.create! :name => "A mug", :code => "PX54"
38
+ @p = Product.create! :name => "A mug", :code => "PX54", :category => (Category.create! :name => "Mugs")
39
+ Color.create! :name => "Blue", :product => @p
29
40
 
30
41
  visit edit_product_path(@p)
31
42
  fill_in "Name", :with => "UA"
@@ -44,5 +55,13 @@ describe "Dejavu" do
44
55
  field_should_have "product_name", "UA"
45
56
  field_should_have "product_code", "PX54"
46
57
  end
58
+
59
+ it "should have the many-to-one association prefilled with the previously entered values" do
60
+ field_should_have "product_category_attributes_name", "Mugs"
61
+ end
62
+
63
+ it "should have the one-to-many association prefilled with the previously entered values" do
64
+ field_should_have "product_colors_attributes_0_name", "Blue"
65
+ end
47
66
  end
48
67
  end
@@ -25,6 +25,8 @@ class ProductsController < ApplicationController
25
25
  # GET /products/new.json
26
26
  def new
27
27
  @product = Product.new
28
+ @product.category = Category.new
29
+ @product.colors.build
28
30
 
29
31
  respond_to do |format|
30
32
  format.html # new.html.erb
@@ -47,7 +49,7 @@ class ProductsController < ApplicationController
47
49
  format.html { redirect_to @product, notice: 'Product was successfully created.' }
48
50
  format.json { render json: @product, status: :created, location: @product }
49
51
  else
50
- save_for_dejavu @product
52
+ save_for_dejavu @product, :nested => [:category, :colors]
51
53
  format.html { redirect_to new_product_url }
52
54
  format.json { render json: @product.errors, status: :unprocessable_entity }
53
55
  end
@@ -0,0 +1,7 @@
1
+ class Category < ActiveRecord::Base
2
+ has_many :products
3
+
4
+ def to_s
5
+ name
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ class Color < ActiveRecord::Base
2
+ belongs_to :product
3
+
4
+ validates_presence_of :product
5
+ end
@@ -1,5 +1,11 @@
1
1
  class Product < ActiveRecord::Base
2
+ belongs_to :category
3
+ has_many :colors
4
+
5
+ validates_presence_of :category
2
6
  validates_presence_of :name, :code
3
7
  validates_length_of :name, :in => 3..10
4
8
  validates_length_of :code, :in => 3..10
9
+
10
+ accepts_nested_attributes_for :category, :colors
5
11
  end
@@ -19,6 +19,18 @@
19
19
  <%= f.label :code %><br />
20
20
  <%= f.text_field :code %>
21
21
  </div>
22
+ <div>
23
+ <%= f.fields_for :category do |ctf| %>
24
+ <%= ctf.label :category_name %>
25
+ <%= ctf.text_field :name %>
26
+ <% end %>
27
+ </div>
28
+ <div>
29
+ <%= f.fields_for :colors do |clf| %>
30
+ <%= clf.label :color_name %>
31
+ <%= clf.text_field :name %>
32
+ <% end %>
33
+ </div>
22
34
  <div class="actions">
23
35
  <%= f.submit %>
24
36
  </div>
@@ -4,6 +4,7 @@
4
4
  <tr>
5
5
  <th>Name</th>
6
6
  <th>Code</th>
7
+ <th>Category</th>
7
8
  <th></th>
8
9
  <th></th>
9
10
  <th></th>
@@ -13,6 +14,7 @@
13
14
  <tr>
14
15
  <td><%= product.name %></td>
15
16
  <td><%= product.code %></td>
17
+ <td><%= product.category %></td>
16
18
  <td><%= link_to 'Show', product %></td>
17
19
  <td><%= link_to 'Edit', edit_product_path(product) %></td>
18
20
  <td><%= link_to 'Destroy', product, confirm: 'Are you sure?', method: :delete %></td>
@@ -10,6 +10,10 @@
10
10
  <%= @product.code %>
11
11
  </p>
12
12
 
13
+ <p>
14
+ <b>Category name:</b>
15
+ <%= @product.category %>
16
+ </p>
13
17
 
14
18
  <%= link_to 'Edit', edit_product_path(@product) %> |
15
19
  <%= link_to 'Back', products_path %>
@@ -0,0 +1,11 @@
1
+ class CreateCategories < ActiveRecord::Migration
2
+ def change
3
+ create_table :categories do |t|
4
+ t.string :name
5
+
6
+ t.timestamps
7
+ end
8
+
9
+ add_column :products, :category_id, :integer
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ class CreateColors < ActiveRecord::Migration
2
+ def change
3
+ create_table :colors do |t|
4
+ t.string :name
5
+ t.integer :product_id
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -11,13 +11,27 @@
11
11
  #
12
12
  # It's strongly recommended to check this file into your version control system.
13
13
 
14
- ActiveRecord::Schema.define(:version => 20111227135944) do
14
+ ActiveRecord::Schema.define(:version => 20120211132313) do
15
+
16
+ create_table "categories", :force => true do |t|
17
+ t.string "name"
18
+ t.datetime "created_at"
19
+ t.datetime "updated_at"
20
+ end
21
+
22
+ create_table "colors", :force => true do |t|
23
+ t.string "name"
24
+ t.integer "product_id"
25
+ t.datetime "created_at"
26
+ t.datetime "updated_at"
27
+ end
15
28
 
16
29
  create_table "products", :force => true do |t|
17
30
  t.string "name"
18
31
  t.string "code"
19
32
  t.datetime "created_at"
20
33
  t.datetime "updated_at"
34
+ t.integer "category_id"
21
35
  end
22
36
 
23
37
  end
@@ -0,0 +1,7 @@
1
+ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
2
+
3
+ one:
4
+ name: MyString
5
+
6
+ two:
7
+ name: MyString
@@ -0,0 +1,9 @@
1
+ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
2
+
3
+ one:
4
+ name: MyString
5
+ product_id: 1
6
+
7
+ two:
8
+ name: MyString
9
+ product_id: 1
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class CategoryTest < ActiveSupport::TestCase
4
+ # test "the truth" do
5
+ # assert true
6
+ # end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class ColorTest < ActiveSupport::TestCase
4
+ # test "the truth" do
5
+ # assert true
6
+ # end
7
+ 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.1.2
4
+ version: 0.2.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-09 00:00:00.000000000 Z
12
+ date: 2012-02-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &79465570 !ruby/object:Gem::Requirement
16
+ requirement: &75307960 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *79465570
24
+ version_requirements: *75307960
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec-rails
27
- requirement: &79499400 !ruby/object:Gem::Requirement
27
+ requirement: &75307000 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '2.7'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *79499400
35
+ version_requirements: *75307000
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: capybara
38
- requirement: &79498830 !ruby/object:Gem::Requirement
38
+ requirement: &75306270 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: 1.1.1
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *79498830
46
+ version_requirements: *75306270
47
47
  description: Remember your object after a redirect
48
48
  email:
49
49
  - roger@itnig.net
@@ -53,6 +53,7 @@ extra_rdoc_files: []
53
53
  files:
54
54
  - .gitignore
55
55
  - .rspec
56
+ - .travis.yml
56
57
  - Gemfile
57
58
  - README.md
58
59
  - Rakefile
@@ -78,6 +79,8 @@ files:
78
79
  - test_app/app/helpers/products_helper.rb
79
80
  - test_app/app/mailers/.gitkeep
80
81
  - test_app/app/models/.gitkeep
82
+ - test_app/app/models/category.rb
83
+ - test_app/app/models/color.rb
81
84
  - test_app/app/models/product.rb
82
85
  - test_app/app/views/layouts/application.html.erb
83
86
  - test_app/app/views/products/_form.html.erb
@@ -102,6 +105,8 @@ files:
102
105
  - test_app/config/locales/en.yml
103
106
  - test_app/config/routes.rb
104
107
  - test_app/db/migrate/20111227135944_create_products.rb
108
+ - test_app/db/migrate/20120211125006_create_categories.rb
109
+ - test_app/db/migrate/20120211132313_create_colors.rb
105
110
  - test_app/db/schema.rb
106
111
  - test_app/db/seeds.rb
107
112
  - test_app/lib/assets/.gitkeep
@@ -114,6 +119,8 @@ files:
114
119
  - test_app/public/robots.txt
115
120
  - test_app/script/rails
116
121
  - test_app/test/fixtures/.gitkeep
122
+ - test_app/test/fixtures/categories.yml
123
+ - test_app/test/fixtures/colors.yml
117
124
  - test_app/test/fixtures/products.yml
118
125
  - test_app/test/functional/.gitkeep
119
126
  - test_app/test/functional/products_controller_test.rb
@@ -121,6 +128,8 @@ files:
121
128
  - test_app/test/performance/browsing_test.rb
122
129
  - test_app/test/test_helper.rb
123
130
  - test_app/test/unit/.gitkeep
131
+ - test_app/test/unit/category_test.rb
132
+ - test_app/test/unit/color_test.rb
124
133
  - test_app/test/unit/helpers/products_helper_test.rb
125
134
  - test_app/test/unit/product_test.rb
126
135
  - test_app/vendor/assets/stylesheets/.gitkeep
@@ -139,7 +148,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
139
148
  version: '0'
140
149
  segments:
141
150
  - 0
142
- hash: 139276129
151
+ hash: 848035681
143
152
  required_rubygems_version: !ruby/object:Gem::Requirement
144
153
  none: false
145
154
  requirements:
@@ -148,7 +157,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
157
  version: '0'
149
158
  segments:
150
159
  - 0
151
- hash: 139276129
160
+ hash: 848035681
152
161
  requirements: []
153
162
  rubyforge_project:
154
163
  rubygems_version: 1.8.15