From 9759a32394a95940c954b4f49a91e231b6f01383 Mon Sep 17 00:00:00 2001 From: Rueshyna Date: Mon, 3 Dec 2012 17:16:28 +0800 Subject: [PATCH] can display relation between author --- .../desktop/co_authors_controller.rb | 2 ++ app/models/user/co_author.rb | 3 +-- app/views/desktop/co_authors/_form.html.erb | 2 +- app/views/desktop/co_authors/index.html.erb | 3 ++- db/seeds.rb | 19 ++++++++++++++++--- 5 files changed, 22 insertions(+), 7 deletions(-) diff --git a/app/controllers/desktop/co_authors_controller.rb b/app/controllers/desktop/co_authors_controller.rb index 5b871bd9..c027a162 100644 --- a/app/controllers/desktop/co_authors_controller.rb +++ b/app/controllers/desktop/co_authors_controller.rb @@ -1,6 +1,7 @@ class Desktop::CoAuthorsController < ApplicationController def index @co_authors = CoAuthor.where(name_id: current_user.id) + @co_author_relations = CoAuthorRelation.all respond_to do |format| format.html { render :layout => false} @@ -27,6 +28,7 @@ class Desktop::CoAuthorsController < ApplicationController def edit @co_author = CoAuthor.find(params[:id]) + @co_author_relations = CoAuthorRelation.all respond_to do |format| format.html { render :layout => false} end diff --git a/app/models/user/co_author.rb b/app/models/user/co_author.rb index 9bcaf7d6..4c662acb 100644 --- a/app/models/user/co_author.rb +++ b/app/models/user/co_author.rb @@ -6,9 +6,8 @@ class CoAuthor field :name_id, type: BSON::ObjectId field :co_author, localize: true field :email - field :type - has_and_belongs_to_many :co_author_relations + belongs_to :co_author_relations VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/ validates :email, format: { with: VALID_EMAIL_REGEX }, diff --git a/app/views/desktop/co_authors/_form.html.erb b/app/views/desktop/co_authors/_form.html.erb index 3628dff3..b087be95 100644 --- a/app/views/desktop/co_authors/_form.html.erb +++ b/app/views/desktop/co_authors/_form.html.erb @@ -17,7 +17,7 @@ <%= f.label :type%>
- <%= options_for_select( @co_author_relations.map do |relation| [relation.relation, relation.id] diff --git a/app/views/desktop/co_authors/index.html.erb b/app/views/desktop/co_authors/index.html.erb index e8e424d2..b01b2569 100644 --- a/app/views/desktop/co_authors/index.html.erb +++ b/app/views/desktop/co_authors/index.html.erb @@ -17,7 +17,8 @@ <%= co_author.co_author %> <%= co_author.email %> - <%= co_author.type%> + <%= @co_author_relations.find(co_author.co_author_relations_id).relation \ + unless co_author.co_author_relations_id.nil?%> <%= link_to 'Edit', edit_desktop_co_author_path(co_author), :class => "bt-edit" %> <%= link_to 'Destroy', desktop_co_author_path(co_author), method: :delete, confirm: 'Are you sure?', :class=>"bt-delete" %> diff --git a/db/seeds.rb b/db/seeds.rb index 00825e32..9c90554c 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -2,8 +2,9 @@ require 'factory_girl' require 'faker' require 'json' -WritingJournal.destroy_all CoAuthor.destroy_all +CoAuthorRelation.destroy_all +WritingJournal.destroy_all data = File.read("db/data") data_json = JSON.parse(data) @@ -16,13 +17,21 @@ name_en = name_en.map do |p| Faker::Name::name end email = Array.new 51,"" email= email.map do |p| Faker::Internet.email end -type = ["friend", "teacher", "student", "mate", "relation"] +type = ["friend0", "teacher0", "student0", "mate0", "relation0", "friend1", "teacher1", "student1", "mate1", "relation1"] + FactoryGirl.define do + factory(:type, class: "CoAuthorRelation") do |f| + f.sequence(:relation_translations) do |n| + { zh_tw: "#{type[n%type.size]}", + en: "#{type[n%type.size]}" } + end + end + factory(:co_author_candidate, class: "CoAuthor") do |f| f.sequence(:co_author_translations) do |n| { zh_tw: "#{name_tw[n]}", - en: "#{name_en[n]}" } + en: "#{name_en[n]}" } end f.sequence(:type) do |n| "#{type[n%5]}" end f.sequence(:email) do |n| "#{email[n]}" end @@ -81,6 +90,9 @@ FactoryGirl.define do end end +10.times.each do + FactoryGirl.create(:type) +end 50.times.each do FactoryGirl.create(:paper_record) @@ -89,3 +101,4 @@ end 50.times.each do FactoryGirl.create(:co_author_candidate) end +