# HG changeset patch # User luisf # Date 1355514600 0 # Node ID 212b72b966ffe48462601d02b275f3c6c9a9eeaf # Parent ca871a83c0f425ba414a312ce3ec4b3d0588759a development commit - changing association validations. not working correctly diff -r ca871a83c0f4 -r 212b72b966ff vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb --- a/vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb Tue Dec 11 18:48:33 2012 +0000 +++ b/vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb Fri Dec 14 19:50:00 2012 +0000 @@ -103,31 +103,35 @@ params[:pub][:authorships].each do |idx| auth = idx[1] - authorship = Authorship.new :name_on_paper => auth[:name_on_paper] - # when there's no association at all if auth[:parent].nil? logger.error { "Creating new author (no assoc)" } - author = Author.new + author = Author.create + else logger.error { "AUTH PARENT #{auth[:parent]}" } parent_class, parent_id = auth[:parent].split "_" if parent_class == "user" user = User.find(parent_id) - author = user.author ||= Author.create(:name => auth[:name_on_paper]) + author = user.author ||= Author.create(:user_id => user.id) else author = Author.find(parent_id) end end - authorship.author_id = author.id - authorship.save! + logger.error { "AAAAA #{auth[:name_on_paper]}" } - @pubication.authors << author + authorship = Authorship.create(:name_on_paper => auth[:name_on_paper], :author_id => author.id) + + @publication.authors << author @publication.authorships << authorship end + + debugger + + if @publication.save @publication.notify_authors_publication_added(@project) diff -r ca871a83c0f4 -r 212b72b966ff vendor/plugins/redmine_bibliography/app/models/author.rb --- a/vendor/plugins/redmine_bibliography/app/models/author.rb Tue Dec 11 18:48:33 2012 +0000 +++ b/vendor/plugins/redmine_bibliography/app/models/author.rb Fri Dec 14 19:50:00 2012 +0000 @@ -1,20 +1,9 @@ class Author < ActiveRecord::Base unloadable - + has_many :authorships, :dependent => :destroy has_many :publications, :through => :authorships - belongs_to :user - - def <=>(author) - name.downcase <=> author.name.downcase - end - - named_scope :like, lambda {|q| - s = "%#{q.to_s.strip.downcase}%" - {:conditions => ["LOWER(name) LIKE :s", {:s => s}], - :order => 'name' - } - } +# validates_length_of :authorships, :minimum => 1, :message => "need to have at least 1 associated authorship - author not saved." end diff -r ca871a83c0f4 -r 212b72b966ff vendor/plugins/redmine_bibliography/app/models/authorship.rb --- a/vendor/plugins/redmine_bibliography/app/models/authorship.rb Tue Dec 11 18:48:33 2012 +0000 +++ b/vendor/plugins/redmine_bibliography/app/models/authorship.rb Fri Dec 14 19:50:00 2012 +0000 @@ -4,15 +4,16 @@ belongs_to :author belongs_to :publication - accepts_nested_attributes_for :author - accepts_nested_attributes_for :publication + # attr_accessor :is_user, :author_user_id, :search_name, :identify_author, :search_results - validates_presence_of :name_on_paper + validates_associated :publication - attr_accessor :is_user, :author_user_id, :search_name, :identify_author, :search_results + validates_presence_of :author, :message => "is not associated with an author. Authorship not saved." + validates_presence_of :name_on_paper, :message => 'cannot be blank: publication not saved.' - # todo: too much logic here - # before_save :associate_author_user + + # todo: remove? + #### before_save :associate_author_user named_scope :like_unique, lambda {|q| s = "%#{q.to_s.strip.downcase}%" @@ -29,12 +30,8 @@ } } - def name - return self.name_on_paper - end - def <=>(authorship) - name.downcase <=> authorship.name.downcase + name_on_paper.downcase <=> authorship.name_on_paper.downcase end def mail @@ -42,32 +39,34 @@ end protected - def associate_author_user - case self.identify_author - when "no" - author = Author.new - author.save - self.author_id = author.id - else - selected = self.search_results - selected_classname = Kernel.const_get(selected.split('_')[0]) - selected_id = selected.split('_')[1] - object = selected_classname.find(selected_id) - if object.respond_to? :name_on_paper - # Authorship - self.author_id = object.author.id - else - # User - unless object.author.nil? - self.author_id = object.author.id - else - author = Author.new - object.author = author - object.save - self.author_id = object.author.id - end - end - end - end + # need to remove this code from this part of the model + #def associate_author_user + # case self.identify_author + # when "no" + # author = Author.new + # author.save + # self.author_id = author.id + # else + # selected = self.search_results + # selected_classname = Kernel.const_get(selected.split('_')[0]) + # selected_id = selected.split('_')[1] + # object = selected_classname.find(selected_id) +# + # if object.respond_to? :name_on_paper + # # Authorship + # self.author_id = object.author.id + # else + # # User + # unless object.author.nil? + # self.author_id = object.author.id + # else + # author = Author.new + # object.author = author + # object.save + # self.author_id = object.author.id + # end + # end + # end + #end end diff -r ca871a83c0f4 -r 212b72b966ff vendor/plugins/redmine_bibliography/app/models/bibtex_entry.rb --- a/vendor/plugins/redmine_bibliography/app/models/bibtex_entry.rb Tue Dec 11 18:48:33 2012 +0000 +++ b/vendor/plugins/redmine_bibliography/app/models/bibtex_entry.rb Fri Dec 14 19:50:00 2012 +0000 @@ -2,7 +2,7 @@ unloadable belongs_to :publication - validates_presence_of :entry_type + validates_presence_of :entry_type, :publication attr_accessible :entry_type, :address, :annote, :booktitle, :chapter, :crossref, :edition, :editor, :eprint, :howpublished, :journal, :key, :month, :note, :number, :organization, :pages, :publisher, :school, :series, :type, :url, :volume, :year diff -r ca871a83c0f4 -r 212b72b966ff vendor/plugins/redmine_bibliography/app/models/publication.rb --- a/vendor/plugins/redmine_bibliography/app/models/publication.rb Tue Dec 11 18:48:33 2012 +0000 +++ b/vendor/plugins/redmine_bibliography/app/models/publication.rb Fri Dec 14 19:50:00 2012 +0000 @@ -6,18 +6,19 @@ has_many :authorships, :dependent => :destroy, :order => "auth_order ASC" has_many :authors, :through => :authorships, :uniq => true - has_one :bibtex_entry, :dependent => :destroy + has_one :bibtex_entry, :dependent => :destroy, :autosave => true validates_presence_of :title - validates_length_of :authorships, :minimum => 1, :message => l("error_no_authors") + validates_length_of :authors, :minimum => 1, :message => l("error_no_authors") - accepts_nested_attributes_for :authorships + accepts_nested_attributes_for :authorships, :allow_destroy => true accepts_nested_attributes_for :authors, :allow_destroy => true - accepts_nested_attributes_for :bibtex_entry, :allow_destroy => true + accepts_nested_attributes_for :bibtex_entry has_and_belongs_to_many :projects, :uniq => true - before_save :set_initial_author_order +# validates_associated :bibtex_entry +# before_save :set_initial_author_order named_scope :visible, lambda {|*args| { :include => :projects, :conditions => Project.allowed_to_condition(args.shift || User.current, :view_publication, *args) } } @@ -66,11 +67,11 @@ def set_initial_author_order authorships = self.authorships - logger.debug { "Publication \"#{self.title}\" has #{authorships.size} authors." } + # logger.debug { "Publication \"#{self.title}\" has #{authorships.size} authors." } authorships.each_with_index do |authorship, index| if authorship.auth_order.nil? - authorship.auth_order = index + authorship.auth_order = index end end end diff -r ca871a83c0f4 -r 212b72b966ff vendor/plugins/redmine_bibliography/config/locales/en.yml --- a/vendor/plugins/redmine_bibliography/config/locales/en.yml Tue Dec 11 18:48:33 2012 +0000 +++ b/vendor/plugins/redmine_bibliography/config/locales/en.yml Fri Dec 14 19:50:00 2012 +0000 @@ -78,7 +78,7 @@ # authorships model field_institution: "Institution" - field_name_on_paper: "Name" + field_name_on_paper: "Name on paper" field_email: "Email Address" # bibtex_entries model