From 5ee2ea8e4845d5099102581c7dc05b8c5d13fee2 Mon Sep 17 00:00:00 2001 From: Meutel Date: Sun, 28 Jun 2015 16:39:18 +0200 Subject: [PATCH] associations book-tag-publisher-language --- app/models/book.rb | 12 ++++++++++++ app/models/book_publisher_link.rb | 8 ++++++++ app/models/language.rb | 8 ++++++++ app/models/publisher.rb | 6 ++++++ app/models/tag.rb | 8 ++++++++ app/views/books/show.html.erb | 13 ++++++++++++- test/fixtures/book_language_links.yml | 11 +++++++++++ test/fixtures/book_publisher_links.yml | 11 +++++++++++ test/fixtures/book_tag_links.yml | 11 +++++++++++ test/fixtures/languages.yml | 11 +++++++++++ test/fixtures/publishers.yml | 11 +++++++++++ test/fixtures/tags.yml | 11 +++++++++++ test/models/book_language_link_test.rb | 7 +++++++ test/models/book_publisher_link_test.rb | 7 +++++++ test/models/book_tag_link_test.rb | 7 +++++++ test/models/language_test.rb | 7 +++++++ test/models/publisher_test.rb | 7 +++++++ test/models/tag_test.rb | 7 +++++++ 18 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 app/models/book_publisher_link.rb create mode 100644 app/models/language.rb create mode 100644 app/models/publisher.rb create mode 100644 app/models/tag.rb create mode 100644 test/fixtures/book_language_links.yml create mode 100644 test/fixtures/book_publisher_links.yml create mode 100644 test/fixtures/book_tag_links.yml create mode 100644 test/fixtures/languages.yml create mode 100644 test/fixtures/publishers.yml create mode 100644 test/fixtures/tags.yml create mode 100644 test/models/book_language_link_test.rb create mode 100644 test/models/book_publisher_link_test.rb create mode 100644 test/models/book_tag_link_test.rb create mode 100644 test/models/language_test.rb create mode 100644 test/models/publisher_test.rb create mode 100644 test/models/tag_test.rb diff --git a/app/models/book.rb b/app/models/book.rb index 6d70287..ccfed92 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -5,6 +5,18 @@ class Book < ActiveRecord::Base join_table: "books_authors_link", foreign_key: "book", association_foreign_key: "author" + has_and_belongs_to_many :tags, + join_table: "books_tags_link", + foreign_key: "book", + association_foreign_key: "tag" + has_and_belongs_to_many :languages, + join_table: "books_languages_link", + foreign_key: "book", + association_foreign_key: "lang_code" + + has_one :book_publisher_link, foreign_key: "book" + has_one :publisher, through: :book_publisher_link + has_one :book_serie_link, foreign_key: "book" has_one :serie, through: :book_serie_link end diff --git a/app/models/book_publisher_link.rb b/app/models/book_publisher_link.rb new file mode 100644 index 0000000..e3cf09a --- /dev/null +++ b/app/models/book_publisher_link.rb @@ -0,0 +1,8 @@ +class BookPublisherLink < ActiveRecord::Base + establish_connection Rails.configuration.database_configuration["calibre"] + + self.table_name = "books_publishers_link" + + belongs_to :book, foreign_key: "book" + belongs_to :publisher, foreign_key: "publisher" +end diff --git a/app/models/language.rb b/app/models/language.rb new file mode 100644 index 0000000..2a4a32e --- /dev/null +++ b/app/models/language.rb @@ -0,0 +1,8 @@ +class Language < ActiveRecord::Base + establish_connection Rails.configuration.database_configuration["calibre"] + + has_and_belongs_to_many :books, + join_table: "books_languages_link", + foreign_key: "lang_code", + association_foreign_key: "book" +end diff --git a/app/models/publisher.rb b/app/models/publisher.rb new file mode 100644 index 0000000..32aea12 --- /dev/null +++ b/app/models/publisher.rb @@ -0,0 +1,6 @@ +class Publisher < ActiveRecord::Base + establish_connection Rails.configuration.database_configuration["calibre"] + + has_many :book_publisher_link, foreign_key: "publishers" + has_many :books, through: :book_publisher_link +end diff --git a/app/models/tag.rb b/app/models/tag.rb new file mode 100644 index 0000000..2ca4183 --- /dev/null +++ b/app/models/tag.rb @@ -0,0 +1,8 @@ +class Tag < ActiveRecord::Base + establish_connection Rails.configuration.database_configuration["calibre"] + + has_and_belongs_to_many :books, + join_table: "books_tags_link", + foreign_key: "tag", + association_foreign_key: "book" +end diff --git a/app/views/books/show.html.erb b/app/views/books/show.html.erb index 8490ce2..2c1b3a4 100644 --- a/app/views/books/show.html.erb +++ b/app/views/books/show.html.erb @@ -4,4 +4,15 @@
  • <%= author.name %>
  • <% end %> -

    Serie: <%= @book.serie.name %> +

    + +

    Serie: <%= @book.serie.name %>

    +

    Publisher: <%= @book.publisher.name %>

    diff --git a/test/fixtures/book_language_links.yml b/test/fixtures/book_language_links.yml new file mode 100644 index 0000000..937a0c0 --- /dev/null +++ b/test/fixtures/book_language_links.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/fixtures/book_publisher_links.yml b/test/fixtures/book_publisher_links.yml new file mode 100644 index 0000000..937a0c0 --- /dev/null +++ b/test/fixtures/book_publisher_links.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/fixtures/book_tag_links.yml b/test/fixtures/book_tag_links.yml new file mode 100644 index 0000000..937a0c0 --- /dev/null +++ b/test/fixtures/book_tag_links.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/fixtures/languages.yml b/test/fixtures/languages.yml new file mode 100644 index 0000000..937a0c0 --- /dev/null +++ b/test/fixtures/languages.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/fixtures/publishers.yml b/test/fixtures/publishers.yml new file mode 100644 index 0000000..937a0c0 --- /dev/null +++ b/test/fixtures/publishers.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/fixtures/tags.yml b/test/fixtures/tags.yml new file mode 100644 index 0000000..937a0c0 --- /dev/null +++ b/test/fixtures/tags.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/models/book_language_link_test.rb b/test/models/book_language_link_test.rb new file mode 100644 index 0000000..22c27a8 --- /dev/null +++ b/test/models/book_language_link_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class BookLanguageLinkTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/book_publisher_link_test.rb b/test/models/book_publisher_link_test.rb new file mode 100644 index 0000000..fea906f --- /dev/null +++ b/test/models/book_publisher_link_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class BookPublisherLinkTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/book_tag_link_test.rb b/test/models/book_tag_link_test.rb new file mode 100644 index 0000000..d8f2015 --- /dev/null +++ b/test/models/book_tag_link_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class BookTagLinkTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/language_test.rb b/test/models/language_test.rb new file mode 100644 index 0000000..cb3f83b --- /dev/null +++ b/test/models/language_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class LanguageTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/publisher_test.rb b/test/models/publisher_test.rb new file mode 100644 index 0000000..a54a2c3 --- /dev/null +++ b/test/models/publisher_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class PublisherTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/tag_test.rb b/test/models/tag_test.rb new file mode 100644 index 0000000..b8498a1 --- /dev/null +++ b/test/models/tag_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class TagTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end