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 @ 1426:7367cd232b1e

History | View | Annotate | Download (3.06 KB)

1 328:aed18b463206 luis
class Authorship < ActiveRecord::Base
2 1123:48c5fdd6cf10 luis
  unloadable
3
4 328:aed18b463206 luis
  belongs_to :author
5
  belongs_to :publication
6 1123:48c5fdd6cf10 luis
7 483:cc267eb99115 luis
  accepts_nested_attributes_for :author
8
  accepts_nested_attributes_for :publication
9 686:b1debf464389 luis
10
  validates_presence_of :name_on_paper
11 1123:48c5fdd6cf10 luis
12 1395:0e4c6c2f400e luis
  attr_writer :search_author_id , :search_author_class
13 1394:0f918e37e1d6 luis
  attr_writer :search_author_tie
14 1364:4d5d25039a5f luis
15 1394:0f918e37e1d6 luis
  ### attr_accessor :search_results, :identify_author
16
  ## attr_writer :search_author_class
17
18 1395:0e4c6c2f400e luis
  before_save :set_author
19 1367:a2e51c0a7860 luis
  before_update :delete_publication_cache
20 591:9e866f13c984 luis
21 1363:855b4ae5ecdd luis
  # tod: review scope of ordering
22
  acts_as_list :column => 'auth_order'
23 1317:2805873c0147 luis
24 1124:807426fa6017 luis
  # todo: review usage of scope --lf.20130108
25 1123:48c5fdd6cf10 luis
  scope :like_unique, lambda {|q|
26 601:1608b3cb50cd luis
    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 1124:807426fa6017 luis
  # todo: review usage of scope --lf.20130108
34 1123:48c5fdd6cf10 luis
  scope :like, lambda {|q|
35 591:9e866f13c984 luis
    s = "%#{q.to_s.strip.downcase}%"
36 601:1608b3cb50cd luis
    {:conditions => ["LOWER(name_on_paper) LIKE :s OR LOWER(email) LIKE :s", {:s => s}],
37 591:9e866f13c984 luis
     :order => 'name_on_paper'
38
    }
39
  }
40 1123:48c5fdd6cf10 luis
41 1394:0f918e37e1d6 luis
  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 1397:bf2db886a543 luis
      aclass = ""
48 1394:0f918e37e1d6 luis
    else
49 1397:bf2db886a543 luis
      aclass = "Author"
50 1394:0f918e37e1d6 luis
    end
51 1395:0e4c6c2f400e luis
52 1397:bf2db886a543 luis
    @search_author_class || aclass
53 1394:0f918e37e1d6 luis
  end
54
55
  def search_author_id
56
    if self.author.nil?
57 1398:92d854be33d5 luis
      authid = ""
58 1394:0f918e37e1d6 luis
    else
59 1397:bf2db886a543 luis
      authid = author_id
60 1394:0f918e37e1d6 luis
    end
61 1397:bf2db886a543 luis
62
    @search_author_id || authid
63 1394:0f918e37e1d6 luis
  end
64
65
  def search_author_tie
66
    if self.author.nil?
67 1397:bf2db886a543 luis
      auth_tie = false
68 1394:0f918e37e1d6 luis
    else
69 1397:bf2db886a543 luis
      auth_tie = true
70 1394:0f918e37e1d6 luis
    end
71
72 1397:bf2db886a543 luis
    @search_author_tie || auth_tie
73 1394:0f918e37e1d6 luis
  end
74
75 592:68c6b060385c luis
  def name
76
    return self.name_on_paper
77
  end
78 1123:48c5fdd6cf10 luis
79 591:9e866f13c984 luis
  def <=>(authorship)
80 592:68c6b060385c luis
    name.downcase <=> authorship.name.downcase
81
  end
82 1123:48c5fdd6cf10 luis
83 592:68c6b060385c luis
  def mail
84
    return self.email
85 591:9e866f13c984 luis
  end
86 1123:48c5fdd6cf10 luis
87
  protected
88 1367:a2e51c0a7860 luis
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 1395:0e4c6c2f400e luis
  private
96
97 1394:0f918e37e1d6 luis
  def set_author
98 1403:35732ac4324a luis
    # 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 1394:0f918e37e1d6 luis
106 1403:35732ac4324a luis
      case @search_author_class
107
      when ""
108
        author = Author.new
109
        author.save
110 1394:0f918e37e1d6 luis
111 1403:35732ac4324a luis
      when "User"
112
        user = User.find(@search_author_id)
113 1394:0f918e37e1d6 luis
114 1403:35732ac4324a luis
        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 1366:7e85f7988ab8 luis
      author = Author.new
133
      author.save
134 1394:0f918e37e1d6 luis
    end
135 1366:7e85f7988ab8 luis
136 1394:0f918e37e1d6 luis
    self.author = author
137 518:b24091590b63 luis
  end
138 328:aed18b463206 luis
end