Browse files

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

… some refactoring
  • Loading branch information...
1 parent 7400639 commit beca31c69bc7536cbe9d649f987e9859de9272ed @HeroicEric committed Feb 24, 2012
View
1 Gemfile
@@ -29,5 +29,6 @@ group :test, :development do
gem 'guard-spin'
gem 'guard-rspec'
gem 'growl'
+ gem 'shoulda-matchers'
gem 'pry'
end
View
2 Gemfile.lock
@@ -154,6 +154,7 @@ GEM
ffi (~> 1.0.9)
multi_json (~> 1.0.4)
rubyzip
+ shoulda-matchers (1.0.0)
slop (2.4.4)
spin (0.4.3)
sprockets (2.1.2)
@@ -197,6 +198,7 @@ DEPENDENCIES
rails (= 3.2.1)
rspec-rails
sass-rails (~> 3.2.3)
+ shoulda-matchers
sqlite3
twitter-bootstrap-rails
uglifier (>= 1.0.3)
View
4 app/controllers/terms_controller.rb
@@ -6,11 +6,12 @@ def show
def new
@glossary = Glossary.find(params[:glossary_id])
@term = @glossary.new_term
+ @definition = @term.build_definition
end
def create
@glossary = Glossary.find(params[:glossary_id])
- @term = @glossary.new_term(params[:term])
+ @term = @glossary.terms.build(params[:term])
if @term.save
redirect_to glossary_path(@glossary),
@@ -37,7 +38,6 @@ def update
def destroy
@term = Term.find(params[:id])
- puts @term.id
@term.destroy
redirect_to glossary_path(@term.glossary), notice: "Term was successfully deleted!"
end
View
3 app/models/definition.rb
@@ -0,0 +1,3 @@
+class Definition < ActiveRecord::Base
+ has_many :terms
+end
View
3 app/models/glossary.rb
@@ -7,7 +7,4 @@ def new_term(*args)
self.terms.create(*args)
end
- def add_term(term)
- terms << term
- end
end
View
3 app/models/term.rb
@@ -2,4 +2,7 @@ class Term < ActiveRecord::Base
validates :name, :definition, presence: true
belongs_to :glossary
+ belongs_to :definition
+
+ accepts_nested_attributes_for :definition
end
View
2 app/views/glossaries/_term.html.haml
@@ -1,3 +1,3 @@
.well
%h3= link_to term.name, glossary_term_path(@glossary, term)
- %p= term.definition
+ %p= term.definition.text
View
15 app/views/terms/_form.html.haml
@@ -1,6 +1,17 @@
+%form
+ .control-group
+ %label What kind of Term are you making?
+ .controls
+ %label.radio
+ %input{type: "radio", name: "typeOptions"}
+ This term is a synonym for an existing term
+
= form_for [@glossary, @term] do |f|
= f.label :name
= f.text_field :name
- = f.label :definition
- = f.text_field :definition
+
+ = f.fields_for :definition, @definition do |builder|
+ = builder.label :text, "Definition"
+ = builder.text_field :text
+
= f.submit class: "btn"
View
2 app/views/terms/show.html.haml
@@ -5,4 +5,4 @@
= button_to "Delete", glossary_term_path(@term), method: :delete, class: "btn btn-danger"
= link_to "Back to Glossary", glossary_path(@glossary), class: "btn"
%h2 Definition
-%p= @term.definition
+%p= @term.definition.text
View
11 db/migrate/20120221215316_create_definitions.rb
@@ -0,0 +1,11 @@
+class CreateDefinitions < ActiveRecord::Migration
+ def change
+ create_table :definitions do |t|
+ t.text :text
+
+ t.timestamps
+ end
+
+ add_column :terms, :definition_id, :integer
+ end
+end
View
13 db/schema.rb
@@ -11,7 +11,13 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120214174541) do
+ActiveRecord::Schema.define(:version => 20120221215316) do
+
+ create_table "definitions", :force => true do |t|
+ t.text "text"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
create_table "glossaries", :force => true do |t|
t.string "name"
@@ -23,8 +29,9 @@
t.string "name"
t.text "definition"
t.integer "glossary_id"
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ t.integer "definition_id"
end
end
View
7 spec/factories.rb
@@ -6,7 +6,12 @@
factory :term do
sequence(:name) { |n| "term #{n}" }
- definition "A woodchuck can chuck as much wood as it wants."
+ association :glossary
+ association :definition
+ end
+
+ factory :definition do
+ text "This is the text for fake definition!"
end
end
View
7 spec/models/definition_spec.rb
@@ -0,0 +1,7 @@
+require 'spec_helper'
+
+describe Definition do
+
+
+
+end
View
76 spec/models/glossary_spec.rb
@@ -1,96 +1,86 @@
require 'spec_helper'
describe Glossary do
- before(:each) do
- @attr = { name: "foo" }
+ let(:glossary) { Glossary.new(attr) }
+ let(:attr) do
+ { name: "foo" }
end
describe "validations" do
it "creates an instance of itself when given valid attributes" do
- @it = Glossary.new(@attr)
- @it.save.should be_true
+ glossary.save.should be_true
end
it "requires a name" do
- @it = Glossary.new(@attr.merge(name: nil))
- @it.should_not be_valid
+ attr.merge!(name: nil)
+ glossary.should_not be_valid
end
it "has the terms attribute" do
- @it = Glossary.new(@attr)
- @it.should respond_to(:terms)
+ glossary.should respond_to(:terms)
end
it "starts with no terms" do
- @it = Glossary.new(@attr)
- @it.terms.should == []
+ glossary.terms.should == []
end
end
describe "#new_term" do
before(:each) do
- @it = Glossary.create(@attr)
+ glossary.save
end
it "returns an instance of Term" do
- @it.new_term.class.should == Term
+ glossary.new_term.class.should == Term
end
it "accepts a hash of attributes for the Term to be made" do
term_attr = Factory.attributes_for(:term)
- term = @it.new_term(term_attr)
+ term = glossary.terms.create(term_attr)
term_attr.each { |k, v| term.send(k.to_sym).should == v }
end
describe "when given valid attributes for a term" do
it "saves newly added term to terms" do
- starting_number = @it.terms.count
- @it.new_term(Factory.attributes_for(:term))
- @it.terms.count.should > starting_number
+ starting_number = glossary.terms.count
+ glossary.new_term(
+ Factory.attributes_for(:term).merge(definition: Factory(:definition))
+ )
+ glossary.terms.count.should > starting_number
end
end
describe "when given invalid attributes for a term" do
it "doesnt add a new term and raises an error" do
- starting_number = @it.terms.count
- lambda { @it.new_term(clowns: "acasca") }.should raise_error
- @it.terms.count.should == starting_number
+ starting_number = glossary.terms.count
+ lambda { glossary.new_term(clowns: "acasca") }.should raise_error
+ glossary.terms.count.should == starting_number
end
end
end
describe "#terms" do
describe "orders terms alphabetically" do
+ before(:each) do
+ glossary.save!
+ end
+
it "returns terms in alphabetical order" do
- @it = Glossary.create(@attr)
- terms = %w{banana orange apple}.map { |n| Factory(:term, name: n) }
- terms.each { |term| @it.add_term(term) }
- @it.terms.map { |term| term.name }.should == %w{apple banana orange}
+ terms = %w{banana orange apple}.map do |name|
+ Factory(:term, name: name, glossary: glossary)
+ end
+ ordered_term_names = glossary.terms.map {|term| term.name}
+ ordered_term_names.should == %w{apple banana orange}
end
it "it treats capitals properly" do
- @it = Glossary.create(@attr)
- terms = %w{banana Banana orange apple}.map { |n| Factory(:term, name: n) }
- terms.each { |term| @it.add_term(term) }
- @it.terms.map { |term| term.name }.should == %w{apple Banana banana orange}
+ terms = %w{banana Banana orange apple}.map do |name|
+ Factory(:term, name: name, glossary: glossary)
+ end
+ ordered_term_names = glossary.terms.map { |term| term.name }
+ ordered_term_names.should == %w{apple Banana banana orange}
end
end
end
- describe "#add_term" do
- before(:each) do
- @it = Glossary.create(@attr)
- @term = Factory(:term)
- @it.add_term(@term)
- end
-
- it "adds a given term to the collection of terms for the glossary" do
- @it.terms.should include(@term)
- end
-
- it "sets the glossary reference for the given term" do
- @term.glossary.should == @it
- end
- end
-
end
View
28 spec/models/term_spec.rb
@@ -2,31 +2,27 @@
describe Term do
- before(:each) do
- @attr = {
+ it { should belong_to :glossary }
+ it { should belong_to :definition }
+
+ let(:term) { Term.new(attr) }
+ let(:attr) do
+ {
name: "foo",
- definition: "a foo is a bar with baz"
+ definition_attributes: Factory.attributes_for(:definition)
}
end
- it "creates an instance of itself when given valid attributes" do
- @it = Term.new(@attr)
- @it.save.should be_true
- end
-
it "requires a name" do
- @it = Term.new(@attr.merge(name: ""))
- @it.should_not be_valid
+ attr.merge!(name: "")
+ term.should_not be_valid
end
it "requires a definition" do
- @it = Term.new(@attr.merge(definition: ""))
- @it.should_not be_valid
+ term.definition = nil
+ term.should_not be_valid
end
- it "has a glossary attribute" do
- @it = Term.new(@attr)
- @it.should respond_to(:glossary)
+ it "accepts nested attributes for definition" do
end
-
end
View
21 spec/requests/glossary_request_spec.rb
@@ -22,34 +22,31 @@
fill_in "Name", with: "foo term"
fill_in "Definition", with: "a foo is a bar with baz"
find('input[type=submit]').click
- current_path.should == glossary_path(glossary)
page.should have_content "foo term"
page.should have_content "a foo is a bar with baz"
end
scenario "Viewing a term" do
- glossary = Factory(:glossary)
- term = glossary.new_term(Factory.attributes_for(:term))
- visit glossary_term_path(glossary, term)
+ term = Factory(:term)
+ visit glossary_term_path(term.glossary, term)
page.should have_content term.name
- page.should have_content term.definition
- page.should have_content "Edit"
+ page.should have_content term.definition.text
+ page.should have_link(
+ "Edit", href: edit_glossary_term_path(term.glossary, term))
end
scenario "Editing a term" do
- glossary = Factory(:glossary)
- term = glossary.new_term(Factory.attributes_for(:term))
- visit glossary_term_path(glossary, term)
+ term = Factory(:term)
+ visit glossary_term_path(term.glossary, term)
click_on "Edit"
fill_in "Name", with: "New Name"
find('input[type=submit]').click
page.should have_css("h1", text: "New Name")
end
scenario "Deleting a term" do
- glossary = Factory(:glossary)
- term = glossary.new_term(Factory.attributes_for(:term))
- visit glossary_term_path(glossary, term)
+ term = Factory(:term)
+ visit glossary_term_path(term.glossary, term)
expect { click_on "Delete" }.should change(Term, :count).by(-1)
page.should have_content "Term was successfully deleted!"
end

0 comments on commit beca31c

Please sign in to comment.