To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / plugins / redmine_bibliography / app / models / authorship.rb @ 1420:b688fe79f593

History | View | Annotate | Download (3.06 KB)

1
class Authorship < ActiveRecord::Base
2
  unloadable
3

    
4
  belongs_to :author
5
  belongs_to :publication
6

    
7
  accepts_nested_attributes_for :author
8
  accepts_nested_attributes_for :publication
9

    
10
  validates_presence_of :name_on_paper
11

    
12
  attr_writer :search_author_id , :search_author_class
13
  attr_writer :search_author_tie
14

    
15
  ### attr_accessor :search_results, :identify_author
16
  ## attr_writer :search_author_class
17

    
18
  before_save :set_author
19
  before_update :delete_publication_cache
20

    
21
  # tod: review scope of ordering
22
  acts_as_list :column => 'auth_order'
23

    
24
  # todo: review usage of scope --lf.20130108
25
  scope :like_unique, lambda {|q|
26
    s = "%#{q.to_s.strip.downcase}%"
27
    {:conditions => ["LOWER(name_on_paper) LIKE :s OR LOWER(email) LIKE :s", {:s => s}],
28
     :order => 'name_on_paper',
29
     :group => "name_on_paper, institution, email"
30
    }
31
  }
32

    
33
  # todo: review usage of scope --lf.20130108
34
  scope :like, lambda {|q|
35
    s = "%#{q.to_s.strip.downcase}%"
36
    {:conditions => ["LOWER(name_on_paper) LIKE :s OR LOWER(email) LIKE :s", {:s => s}],
37
     :order => 'name_on_paper'
38
    }
39
  }
40

    
41
  def search_author_class
42
    # Authorship must always have an Author
43
    # unless it hasn't been saved yet
44
    # using default setter (attr_writer)
45

    
46
    if self.author.nil?
47
      aclass = ""
48
    else
49
      aclass = "Author"
50
    end
51

    
52
    @search_author_class || aclass
53
  end
54

    
55
  def search_author_id
56
    if self.author.nil?
57
      authid = ""
58
    else
59
      authid = author_id
60
    end
61

    
62
    @search_author_id || authid
63
  end
64

    
65
  def search_author_tie
66
    if self.author.nil?
67
      auth_tie = false
68
    else
69
      auth_tie = true
70
    end
71

    
72
    @search_author_tie || auth_tie
73
  end
74

    
75
  def name
76
    return self.name_on_paper
77
  end
78

    
79
  def <=>(authorship)
80
    name.downcase <=> authorship.name.downcase
81
  end
82

    
83
  def mail
84
    return self.email
85
  end
86

    
87
  protected
88

    
89
  def delete_publication_cache
90
    publication = Publication.find(self.publication_id)
91
    Rails.cache.delete "publication-#{publication.id}-ieee"
92
    Rails.cache.delete "publication-#{publication.id}-bibtex"
93
  end
94

    
95
  private
96

    
97
  def set_author
98
    # do we want to associate the authorship
99
    #  with an existing author/user?
100
    if @search_author_tie
101
      # if an author, simply associates with it
102
      # if an user, checks if it has already an author associated with it
103
      #   if so, associates with that author
104
      #   otherwise, creates a new author
105

    
106
      case @search_author_class
107
      when ""
108
        author = Author.new
109
        author.save
110

    
111
      when "User"
112
        user = User.find(@search_author_id)
113

    
114
        if user.author.nil?
115
          # User w/o author:
116
          # create new author and update user
117
          author = Author.new
118
          author.save
119
          user.author = author
120
          user.save
121
        else
122
          author = user.author
123
        end
124

    
125
      when "Author"
126
        author = Author.find(@search_author_id)
127
      end
128

    
129
    # if we don't want to associate with an existing author/user
130
    else
131
      # todo: should we delete any previously existing relationship?
132
      author = Author.new
133
      author.save
134
    end
135

    
136
    self.author = author
137
  end
138
end