Skip to content
This repository has been archived by the owner on Nov 22, 2017. It is now read-only.

Commit

Permalink
Convert ChapterNote#chapter_ids to string
Browse files Browse the repository at this point in the history
Add spec for Chapters#index
  • Loading branch information
saulius committed Aug 9, 2013
1 parent 5c33e29 commit 3f46eae
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/v1/chapters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ChaptersController < ApiController
before_filter :find_chapter, only: [:show]

def index
@chapters = Chapter.all
@chapters = Chapter.eager(:chapter_note).all

respond_with @chapters
end
Expand Down
4 changes: 1 addition & 3 deletions app/models/chapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ class Chapter < GoodsNomenclature
.where(Sequel.~(goods_nomenclatures__goods_nomenclature_item_id: HiddenGoodsNomenclature.codes))
}

one_to_one :chapter_note, dataset: -> {
ChapterNote.where(chapter_id: to_param)
}
one_to_one :chapter_note, primary_key: :to_param

# Tire configuration
tire do
Expand Down
2 changes: 1 addition & 1 deletion app/models/chapter_note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ def validate
end

def chapter_goods_id
sprintf("%02d00000000", chapter_id)
chapter_id.ljust('0', 10)
end
end
3 changes: 3 additions & 0 deletions app/views/api/v1/chapters/index.json.rabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
collection @chapters
attributes :goods_nomenclature_item_id
node(:chapter_note_id) { |chapter| chapter.chapter_note.try(:id) }
18 changes: 18 additions & 0 deletions db/migrate/20130809075350_change_chapter_note_foreign_key_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Sequel.migration do
up do
alter_table :chapter_notes do
set_column_type :chapter_id, String, size: 2
end

# prefix with leading zero chapters with id < 10
self[:chapter_notes].all do |chapter_note|
self[:chapter_notes].filter(content: chapter_note[:content]).update(chapter_id: sprintf("%02d", chapter_note[:chapter_id].to_i))
end
end

down do
alter_table :chapter_notes do
set_column_type :chapter_id, Integer
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{
id: Integer,
section_id: nil,
chapter_id: Integer,
chapter_id: String,
content: String
}
}
Expand Down Expand Up @@ -52,7 +52,7 @@
it 'returns chapter_note attributes' do
pattern = {
id: Integer,
chapter_id: Integer,
chapter_id: String,
section_id: nil,
content: String
}
Expand Down
20 changes: 20 additions & 0 deletions spec/controllers/api/v1/chapters_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,23 @@
end
end
end

describe Api::V1::ChaptersController, "GET #index" do
render_views

let!(:chapter1) { create :chapter, :with_section, :with_note }
let!(:chapter2) { create :chapter, :with_section, :with_note }

let(:pattern) {
[
{goods_nomenclature_item_id: String, chapter_note_id: Integer },
{goods_nomenclature_item_id: String, chapter_note_id: Integer }
]
}

it 'returns rendered records' do
get :index, format: :json

response.body.should match_json_expression pattern
end
end

0 comments on commit 3f46eae

Please sign in to comment.