Class: ReferencesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/references_controller.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) create



45
46
47
48
# File 'app/controllers/references_controller.rb', line 45

def create
  @reference = new_reference
  save true
end

- (Object) destroy



83
84
85
86
87
88
89
90
91
# File 'app/controllers/references_controller.rb', line 83

def destroy
  @reference = Reference.find(params[:id])
  if @reference.destroy
    json = {:success => true}
  else
    json = {:success => false, :message => @reference.errors[:base]}.to_json
  end
  render :json => json
end

- (Object) download



36
37
38
39
40
41
42
43
# File 'app/controllers/references_controller.rb', line 36

def download
  document = ReferenceDocument.find params[:id]
  if document.downloadable_by? current_user
    redirect_to document.actual_url
  else
    head 401
  end
end

- (Object) index



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/references_controller.rb', line 6

def index
  searching = params[:q].present?
  params[:search_selector] ||= 'Search for'
  if ['review', 'new', 'clear'].include? params[:commit]
    params[:q] = ''
  end
  params[:q].strip! if params[:q]
  params[:review] = params[:commit] == 'review'
  params[:whats_new] = params[:commit] == 'new'

  @endnote_export_confirmation_message = <<EOS
AntCat will download these references to a file named "antcat_references.utf8.endnote_import". When your browser asks, save this file. Then use EndNote's Import function on its File menu to import the file. Choose "EndNote Import" from Import Options, and "Unicode (UTF-8)" as the Text Translation.
EOS
  unless searching
    @endnote_export_confirmation_message << "\nSince there are no search criteria, AntCat will download all ten thousand references. This will take several minutes."
  end

  params[:is_author_search] = params[:search_selector] == 'Search for author(s)'

  respond_to do |format|
    format.html   {
      @references = Reference.do_search params
    }
    format.endnote_import  {
      references = Reference.do_search params.merge :format => :endnote_import
      render text: Exporters::Endnote::Formatter.format(references)
    }
  end
end

- (Object) save(new)



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/references_controller.rb', line 55

def save new
  begin
    Reference.transaction do
      clear_document_params_if_necessary
      clear_nested_reference_id unless @reference.kind_of? NestedReference
      parse_author_names_string
      set_journal if @reference.kind_of? ArticleReference
      set_publisher if @reference.kind_of? BookReference
      set_pagination
      # kludge around Rails 3 behavior that uses the type to look up a record - so you can't update the type!
      Reference.connection.execute "UPDATE `references` SET type = '#{@reference.type}' WHERE id = '#{@reference.id}'" unless new
      @reference.update_attributes params[:reference]

      @possible_duplicate = @reference.check_for_duplicate unless params[:possible_duplicate].present?
      unless @possible_duplicate
        @reference.save!
        set_document_host
      end

      raise ActiveRecord::RecordInvalid.new @reference unless @reference.errors.empty?
    end
  rescue ActiveRecord::RecordInvalid
    @reference[:id] = nil if new
    @reference.instance_variable_set :@new_record, new
  end
  render_json new
end

- (Object) update



50
51
52
53
# File 'app/controllers/references_controller.rb', line 50

def update
  @reference = get_reference
  save false
end