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 @ 1395:0e4c6c2f400e

History | View | Annotate | Download (3.19 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 1395:0e4c6c2f400e luis
      @search_author_class = ""
48 1394:0f918e37e1d6 luis
    else
49 1395:0e4c6c2f400e luis
      @search_author_class = "Author"
50 1394:0f918e37e1d6 luis
    end
51 1395:0e4c6c2f400e luis
52
    @search_author_class
53 1394:0f918e37e1d6 luis
  end
54
55 1395:0e4c6c2f400e luis
  # def search_author_class=(search_author_class)
56
  #  @search_author_class = search_author_class
57
  # end
58
59 1394:0f918e37e1d6 luis
  def search_author_id
60
    if self.author.nil?
61 1395:0e4c6c2f400e luis
      "xxx"
62 1394:0f918e37e1d6 luis
    else
63 1395:0e4c6c2f400e luis
      author_id
64 1394:0f918e37e1d6 luis
    end
65
  end
66
67
  def search_author_tie
68
    if self.author.nil?
69
      return false
70
    else
71
      return true
72
    end
73
74
  end
75
76 592:68c6b060385c luis
  def name
77
    return self.name_on_paper
78
  end
79 1123:48c5fdd6cf10 luis
80 591:9e866f13c984 luis
  def <=>(authorship)
81 592:68c6b060385c luis
    name.downcase <=> authorship.name.downcase
82
  end
83 1123:48c5fdd6cf10 luis
84 592:68c6b060385c luis
  def mail
85
    return self.email
86 591:9e866f13c984 luis
  end
87 1123:48c5fdd6cf10 luis
88
  protected
89 1367:a2e51c0a7860 luis
90
  def delete_publication_cache
91
    publication = Publication.find(self.publication_id)
92
    Rails.cache.delete "publication-#{publication.id}-ieee"
93
    Rails.cache.delete "publication-#{publication.id}-bibtex"
94
  end
95
96 1395:0e4c6c2f400e luis
  private
97
98 1394:0f918e37e1d6 luis
  def set_author
99
    # if an author, simply associates with it
100
    # if an user, checks if it has already an author associated with it
101
    # if so, assicoates with that author
102
    # otherwise, creates a new author
103
104
    logger.error { "%%%%%%%%%%%%%%% Associate Author User %%%%%%%%%%%%%%" }
105
106 1395:0e4c6c2f400e luis
    logger.error { "Me #{self.to_yaml}" }
107
    logger.error { "Class: #{@search_author_class}" }
108
    logger.error { "ID #{@search_author_id}" }
109 1394:0f918e37e1d6 luis
110 1395:0e4c6c2f400e luis
    case @search_author_class
111 1366:7e85f7988ab8 luis
    when ""
112 1394:0f918e37e1d6 luis
      logger.debug { "Adding new author to the database." }
113 1366:7e85f7988ab8 luis
      author = Author.new
114
      author.save
115 1394:0f918e37e1d6 luis
116
    when "User"
117
      # get user id
118 1395:0e4c6c2f400e luis
      user = User.find(@search_author_id)
119 1394:0f918e37e1d6 luis
      logger.error { "Found user with this ID: #{user.id}" }
120
121
      if user.author.nil?
122
        logger.error { "The user has no author... creating one!" }
123
124
        # User w/o author:
125
        # create new author and update user
126
        author = Author.new
127
        author.save
128
        user << author
129
      else
130
        logger.error { "found an author!" }
131
        author = user.author
132
      end
133 1366:7e85f7988ab8 luis
134
    when "Author"
135 1395:0e4c6c2f400e luis
      author = Author.find(@search_author_id)
136 1394:0f918e37e1d6 luis
    end
137 1366:7e85f7988ab8 luis
138 1394:0f918e37e1d6 luis
    self.author = author
139 518:b24091590b63 luis
  end
140 328:aed18b463206 luis
end