Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Moved definition out of Term class and into its own class, along with…

… some refactoring
  • Loading branch information...
commit beca31c69bc7536cbe9d649f987e9859de9272ed 1 parent 7400639
Eric Kelly authored
1  Gemfile
@@ -29,5 +29,6 @@ group :test, :development do
29 29 gem 'guard-spin'
30 30 gem 'guard-rspec'
31 31 gem 'growl'
  32 + gem 'shoulda-matchers'
32 33 gem 'pry'
33 34 end
2  Gemfile.lock
@@ -154,6 +154,7 @@ GEM
154 154 ffi (~> 1.0.9)
155 155 multi_json (~> 1.0.4)
156 156 rubyzip
  157 + shoulda-matchers (1.0.0)
157 158 slop (2.4.4)
158 159 spin (0.4.3)
159 160 sprockets (2.1.2)
@@ -197,6 +198,7 @@ DEPENDENCIES
197 198 rails (= 3.2.1)
198 199 rspec-rails
199 200 sass-rails (~> 3.2.3)
  201 + shoulda-matchers
200 202 sqlite3
201 203 twitter-bootstrap-rails
202 204 uglifier (>= 1.0.3)
4 app/controllers/terms_controller.rb
@@ -6,11 +6,12 @@ def show
6 6 def new
7 7 @glossary = Glossary.find(params[:glossary_id])
8 8 @term = @glossary.new_term
  9 + @definition = @term.build_definition
9 10 end
10 11
11 12 def create
12 13 @glossary = Glossary.find(params[:glossary_id])
13   - @term = @glossary.new_term(params[:term])
  14 + @term = @glossary.terms.build(params[:term])
14 15
15 16 if @term.save
16 17 redirect_to glossary_path(@glossary),
@@ -37,7 +38,6 @@ def update
37 38
38 39 def destroy
39 40 @term = Term.find(params[:id])
40   - puts @term.id
41 41 @term.destroy
42 42 redirect_to glossary_path(@term.glossary), notice: "Term was successfully deleted!"
43 43 end
3  app/models/definition.rb
... ... @@ -0,0 +1,3 @@
  1 +class Definition < ActiveRecord::Base
  2 + has_many :terms
  3 +end
3  app/models/glossary.rb
@@ -7,7 +7,4 @@ def new_term(*args)
7 7 self.terms.create(*args)
8 8 end
9 9
10   - def add_term(term)
11   - terms << term
12   - end
13 10 end
3  app/models/term.rb
@@ -2,4 +2,7 @@ class Term < ActiveRecord::Base
2 2 validates :name, :definition, presence: true
3 3
4 4 belongs_to :glossary
  5 + belongs_to :definition
  6 +
  7 + accepts_nested_attributes_for :definition
5 8 end
2  app/views/glossaries/_term.html.haml
... ... @@ -1,3 +1,3 @@
1 1 .well
2 2 %h3= link_to term.name, glossary_term_path(@glossary, term)
3   - %p= term.definition
  3 + %p= term.definition.text
15 app/views/terms/_form.html.haml
... ... @@ -1,6 +1,17 @@
  1 +%form
  2 + .control-group
  3 + %label What kind of Term are you making?
  4 + .controls
  5 + %label.radio
  6 + %input{type: "radio", name: "typeOptions"}
  7 + This term is a synonym for an existing term
  8 +
1 9 = form_for [@glossary, @term] do |f|
2 10 = f.label :name
3 11 = f.text_field :name
4   - = f.label :definition
5   - = f.text_field :definition
  12 +
  13 + = f.fields_for :definition, @definition do |builder|
  14 + = builder.label :text, "Definition"
  15 + = builder.text_field :text
  16 +
6 17 = f.submit class: "btn"
2  app/views/terms/show.html.haml
@@ -5,4 +5,4 @@
5 5 = button_to "Delete", glossary_term_path(@term), method: :delete, class: "btn btn-danger"
6 6 = link_to "Back to Glossary", glossary_path(@glossary), class: "btn"
7 7 %h2 Definition
8   -%p= @term.definition
  8 +%p= @term.definition.text
11 db/migrate/20120221215316_create_definitions.rb
... ... @@ -0,0 +1,11 @@
  1 +class CreateDefinitions < ActiveRecord::Migration
  2 + def change
  3 + create_table :definitions do |t|
  4 + t.text :text
  5 +
  6 + t.timestamps
  7 + end
  8 +
  9 + add_column :terms, :definition_id, :integer
  10 + end
  11 +end
13 db/schema.rb
@@ -11,7 +11,13 @@
11 11 #
12 12 # It's strongly recommended to check this file into your version control system.
13 13
14   -ActiveRecord::Schema.define(:version => 20120214174541) do
  14 +ActiveRecord::Schema.define(:version => 20120221215316) do
  15 +
  16 + create_table "definitions", :force => true do |t|
  17 + t.text "text"
  18 + t.datetime "created_at", :null => false
  19 + t.datetime "updated_at", :null => false
  20 + end
15 21
16 22 create_table "glossaries", :force => true do |t|
17 23 t.string "name"
@@ -23,8 +29,9 @@
23 29 t.string "name"
24 30 t.text "definition"
25 31 t.integer "glossary_id"
26   - t.datetime "created_at", :null => false
27   - t.datetime "updated_at", :null => false
  32 + t.datetime "created_at", :null => false
  33 + t.datetime "updated_at", :null => false
  34 + t.integer "definition_id"
28 35 end
29 36
30 37 end
7 spec/factories.rb
@@ -6,7 +6,12 @@
6 6
7 7 factory :term do
8 8 sequence(:name) { |n| "term #{n}" }
9   - definition "A woodchuck can chuck as much wood as it wants."
  9 + association :glossary
  10 + association :definition
  11 + end
  12 +
  13 + factory :definition do
  14 + text "This is the text for fake definition!"
10 15 end
11 16
12 17 end
7 spec/models/definition_spec.rb
... ... @@ -0,0 +1,7 @@
  1 +require 'spec_helper'
  2 +
  3 +describe Definition do
  4 +
  5 +
  6 +
  7 +end
76 spec/models/glossary_spec.rb
... ... @@ -1,96 +1,86 @@
1 1 require 'spec_helper'
2 2
3 3 describe Glossary do
4   - before(:each) do
5   - @attr = { name: "foo" }
  4 + let(:glossary) { Glossary.new(attr) }
  5 + let(:attr) do
  6 + { name: "foo" }
6 7 end
7 8
8 9 describe "validations" do
9 10 it "creates an instance of itself when given valid attributes" do
10   - @it = Glossary.new(@attr)
11   - @it.save.should be_true
  11 + glossary.save.should be_true
12 12 end
13 13
14 14 it "requires a name" do
15   - @it = Glossary.new(@attr.merge(name: nil))
16   - @it.should_not be_valid
  15 + attr.merge!(name: nil)
  16 + glossary.should_not be_valid
17 17 end
18 18
19 19 it "has the terms attribute" do
20   - @it = Glossary.new(@attr)
21   - @it.should respond_to(:terms)
  20 + glossary.should respond_to(:terms)
22 21 end
23 22
24 23 it "starts with no terms" do
25   - @it = Glossary.new(@attr)
26   - @it.terms.should == []
  24 + glossary.terms.should == []
27 25 end
28 26 end
29 27
30 28 describe "#new_term" do
31 29 before(:each) do
32   - @it = Glossary.create(@attr)
  30 + glossary.save
33 31 end
34 32
35 33 it "returns an instance of Term" do
36   - @it.new_term.class.should == Term
  34 + glossary.new_term.class.should == Term
37 35 end
38 36
39 37 it "accepts a hash of attributes for the Term to be made" do
40 38 term_attr = Factory.attributes_for(:term)
41   - term = @it.new_term(term_attr)
  39 + term = glossary.terms.create(term_attr)
42 40 term_attr.each { |k, v| term.send(k.to_sym).should == v }
43 41 end
44 42
45 43 describe "when given valid attributes for a term" do
46 44 it "saves newly added term to terms" do
47   - starting_number = @it.terms.count
48   - @it.new_term(Factory.attributes_for(:term))
49   - @it.terms.count.should > starting_number
  45 + starting_number = glossary.terms.count
  46 + glossary.new_term(
  47 + Factory.attributes_for(:term).merge(definition: Factory(:definition))
  48 + )
  49 + glossary.terms.count.should > starting_number
50 50 end
51 51 end
52 52
53 53 describe "when given invalid attributes for a term" do
54 54 it "doesnt add a new term and raises an error" do
55   - starting_number = @it.terms.count
56   - lambda { @it.new_term(clowns: "acasca") }.should raise_error
57   - @it.terms.count.should == starting_number
  55 + starting_number = glossary.terms.count
  56 + lambda { glossary.new_term(clowns: "acasca") }.should raise_error
  57 + glossary.terms.count.should == starting_number
58 58 end
59 59 end
60 60 end
61 61
62 62 describe "#terms" do
63 63 describe "orders terms alphabetically" do
  64 + before(:each) do
  65 + glossary.save!
  66 + end
  67 +
64 68 it "returns terms in alphabetical order" do
65   - @it = Glossary.create(@attr)
66   - terms = %w{banana orange apple}.map { |n| Factory(:term, name: n) }
67   - terms.each { |term| @it.add_term(term) }
68   - @it.terms.map { |term| term.name }.should == %w{apple banana orange}
  69 + terms = %w{banana orange apple}.map do |name|
  70 + Factory(:term, name: name, glossary: glossary)
  71 + end
  72 + ordered_term_names = glossary.terms.map {|term| term.name}
  73 + ordered_term_names.should == %w{apple banana orange}
69 74 end
70 75
71 76 it "it treats capitals properly" do
72   - @it = Glossary.create(@attr)
73   - terms = %w{banana Banana orange apple}.map { |n| Factory(:term, name: n) }
74   - terms.each { |term| @it.add_term(term) }
75   - @it.terms.map { |term| term.name }.should == %w{apple Banana banana orange}
  77 + terms = %w{banana Banana orange apple}.map do |name|
  78 + Factory(:term, name: name, glossary: glossary)
  79 + end
  80 + ordered_term_names = glossary.terms.map { |term| term.name }
  81 + ordered_term_names.should == %w{apple Banana banana orange}
76 82 end
77 83 end
78 84 end
79 85
80   - describe "#add_term" do
81   - before(:each) do
82   - @it = Glossary.create(@attr)
83   - @term = Factory(:term)
84   - @it.add_term(@term)
85   - end
86   -
87   - it "adds a given term to the collection of terms for the glossary" do
88   - @it.terms.should include(@term)
89   - end
90   -
91   - it "sets the glossary reference for the given term" do
92   - @term.glossary.should == @it
93   - end
94   - end
95   -
96 86 end
28 spec/models/term_spec.rb
@@ -2,31 +2,27 @@
2 2
3 3 describe Term do
4 4
5   - before(:each) do
6   - @attr = {
  5 + it { should belong_to :glossary }
  6 + it { should belong_to :definition }
  7 +
  8 + let(:term) { Term.new(attr) }
  9 + let(:attr) do
  10 + {
7 11 name: "foo",
8   - definition: "a foo is a bar with baz"
  12 + definition_attributes: Factory.attributes_for(:definition)
9 13 }
10 14 end
11 15
12   - it "creates an instance of itself when given valid attributes" do
13   - @it = Term.new(@attr)
14   - @it.save.should be_true
15   - end
16   -
17 16 it "requires a name" do
18   - @it = Term.new(@attr.merge(name: ""))
19   - @it.should_not be_valid
  17 + attr.merge!(name: "")
  18 + term.should_not be_valid
20 19 end
21 20
22 21 it "requires a definition" do
23   - @it = Term.new(@attr.merge(definition: ""))
24   - @it.should_not be_valid
  22 + term.definition = nil
  23 + term.should_not be_valid
25 24 end
26 25
27   - it "has a glossary attribute" do
28   - @it = Term.new(@attr)
29   - @it.should respond_to(:glossary)
  26 + it "accepts nested attributes for definition" do
30 27 end
31   -
32 28 end
21 spec/requests/glossary_request_spec.rb
@@ -22,24 +22,22 @@
22 22 fill_in "Name", with: "foo term"
23 23 fill_in "Definition", with: "a foo is a bar with baz"
24 24 find('input[type=submit]').click
25   - current_path.should == glossary_path(glossary)
26 25 page.should have_content "foo term"
27 26 page.should have_content "a foo is a bar with baz"
28 27 end
29 28
30 29 scenario "Viewing a term" do
31   - glossary = Factory(:glossary)
32   - term = glossary.new_term(Factory.attributes_for(:term))
33   - visit glossary_term_path(glossary, term)
  30 + term = Factory(:term)
  31 + visit glossary_term_path(term.glossary, term)
34 32 page.should have_content term.name
35   - page.should have_content term.definition
36   - page.should have_content "Edit"
  33 + page.should have_content term.definition.text
  34 + page.should have_link(
  35 + "Edit", href: edit_glossary_term_path(term.glossary, term))
37 36 end
38 37
39 38 scenario "Editing a term" do
40   - glossary = Factory(:glossary)
41   - term = glossary.new_term(Factory.attributes_for(:term))
42   - visit glossary_term_path(glossary, term)
  39 + term = Factory(:term)
  40 + visit glossary_term_path(term.glossary, term)
43 41 click_on "Edit"
44 42 fill_in "Name", with: "New Name"
45 43 find('input[type=submit]').click
@@ -47,9 +45,8 @@
47 45 end
48 46
49 47 scenario "Deleting a term" do
50   - glossary = Factory(:glossary)
51   - term = glossary.new_term(Factory.attributes_for(:term))
52   - visit glossary_term_path(glossary, term)
  48 + term = Factory(:term)
  49 + visit glossary_term_path(term.glossary, term)
53 50 expect { click_on "Delete" }.should change(Term, :count).by(-1)
54 51 page.should have_content "Term was successfully deleted!"
55 52 end

0 comments on commit beca31c

Please sign in to comment.
Something went wrong with that request. Please try again.