Class: SourcesController

Inherits:
ApplicationController show all
Includes:
DataControllerConfiguration::SharedDataControllerConfiguration
Defined in:
app/controllers/sources_controller.rb

Instance Attribute Summary

Attributes inherited from ApplicationController

#is_data_controller, #is_task_controller, #meta_data, #meta_description, #meta_keywords, #meta_title, #page_title, #site_name

Instance Method Summary (collapse)

Methods included from DataControllerConfiguration::SharedDataControllerConfiguration

#set_is_shared_data_model

Methods inherited from ApplicationController

#clear_project_and_user_variables, #digest_cookie, #digested_cookie_exists?, #disable_turbolinks, #intercept_api, #invalid_object, #is_data_controller?, #is_task_controller?, #log_user_recent_route, #notice_user, #project_matches, #record_not_found, #set_project_and_user_variables, #set_project_from_params, #token_authenticate

Methods included from ProjectsHelper

#project_link, #project_tag, #projects_list, #projects_search_form

Instance Method Details

- (Object) autocomplete



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/sources_controller.rb', line 93

def autocomplete
  @sources = Queries::SourceFilterQuery.new(params[:term]).autocomplete
  data = @sources.collect do |t|
    {id: t.id,
     label: ApplicationController.helpers.source_tag(t),
     response_values: {
       params[:method] => t.id
     },
     label_html: ApplicationController.helpers.source_tag(t)
    }
  end
  render :json => data
end

- (Object) batch_load

GET /sources/batch_load This is deprecated



116
117
# File 'app/controllers/sources_controller.rb', line 116

def batch_load
end

- (Object) batch_params (private)



166
167
# File 'app/controllers/sources_controller.rb', line 166

def batch_params
end

- (Object) create

POST /sources POST /sources.json



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/sources_controller.rb', line 33

def create
  @source = Source.new(source_params)

  respond_to do |format|
    if @source.save
      case @source.type
        when 'Source::Bibtex'
          format.html {redirect_to @source.metamorphosize, notice: "Source by '#{@source.author}' was successfully created."}
        when 'Source::Verbatim'
          format.html {redirect_to @source.metamorphosize, notice: "Source '#{@source.cached}' was successfully created."}
        else # type human
          format.html {redirect_to @source.metamorphosize, notice: "Source '#{@source.cached_author_string}' was successfully created."}
      end
      format.json {render action: 'show', status: :created, location: @source}
    else
      if @source.type == 'Source::Bibtex' && source_params['roles_attributes'].try(:count).to_i > 0
        # has an author or editor so force create...
        if @source.errors.get(:base).include?('Missing core data. A TaxonWorks source must have one of the following: author, editor, booktitle, title, url, journal, year, or stated year')
          @source.title = 'forced'
          if @source.save
            @source.title = ''
            @source.save #TODO may need to add a test to confirm it saves the second time.
            format.html {redirect_to @source.metamorphosize, notice: "Source by '#{@source.author}' was successfully created."}
          else
            format.html {render action: 'new'}
            format.json {render json: @source.errors, status: :unprocessable_entity}
          end
        end
      else
        format.html {render action: 'new'}
        format.json {render json: @source.errors, status: :unprocessable_entity}
      end
    end
  end
end

- (Object) create_bibtex_batch_load



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/controllers/sources_controller.rb', line 133

def create_bibtex_batch_load
  file = params.require(:file) 
  redirect_to batch_load_sources_path, notice: 'no file has been selected' and return if file.blank? 
  sha256 = Digest::SHA256.file(file.tempfile)
  if cookies[:batch_sources_md5] == sha256.hexdigest
    if result_hash = Source.batch_create(file.tempfile)
      # error in results?  
      @count = result_hash[:count]
      @sources = result_hash[:records]
      flash[:notice] = "Successfully batch created #{@count} sources."
      render 'sources/batch_load/bibtex_batch_create' 
    else
      flash[:notice] = 'Failed to create sources.'
      redirect_to batch_load_sources_path
    end
  else
    flash[:notice] = 'Batch upload must be previewed before it can be created.'
    redirect_to batch_load_sources_path
  end
end

- (Object) destroy

DELETE /sources/1 DELETE /sources/1.json



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

def destroy
  @source.destroy
  respond_to do |format|
    format.html {redirect_to sources_url}
    format.json {head :no_content}
  end
end

- (Object) download

GET /sources/download



155
156
157
# File 'app/controllers/sources_controller.rb', line 155

def download
  send_data Download.generate_csv(Source.all), type: 'text', filename: "sources_#{DateTime.now.to_s}.csv"
end

- (Object) edit

GET /sources/1/edit



28
29
# File 'app/controllers/sources_controller.rb', line 28

def edit
end

- (Object) index

GET /sources GET /sources.json



8
9
10
11
# File 'app/controllers/sources_controller.rb', line 8

def index
  @recent_objects = Source.created_this_week.order(updated_at: :desc).limit(10)
  render '/shared/data/all/index'
end

- (Object) list



13
14
15
# File 'app/controllers/sources_controller.rb', line 13

def list
  @sources = Source.page(params[:page])
end

- (Object) new

GET /sources/new



23
24
25
# File 'app/controllers/sources_controller.rb', line 23

def new
  @source = Source.new
end

- (Object) preview_bibtex_batch_load



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/controllers/sources_controller.rb', line 119

def preview_bibtex_batch_load
  file = params.require(:file)
  redirect_to batch_load_sources_path, notice: 'No file has been selected.' and return if file.blank?
  file_ok, mimetype = Utilities::Files.recognized_batch_file_type?(file.tempfile)
  if !file_ok 
    redirect_to batch_load_sources_path, notice: "File '#{file.original_filename}' is of type '#{mimetype}', and not processable as BibTex."
  else
    @sources = Source.batch_preview(file.tempfile)
    sha256 = Digest::SHA256.file(file.tempfile)
    cookies[:batch_sources_md5] = sha256.hexdigest
    render 'sources/batch_load/bibtex_batch_preview'
  end
end

- (Object) search



107
108
109
110
111
112
113
# File 'app/controllers/sources_controller.rb', line 107

def search
  if params[:id].blank?
    redirect_to sources_path, notice: 'You must select an item from the list with a click or tab press before clicking show.'
  else
    redirect_to source_path(params[:id])
  end
end

- (Object) set_source (private)



161
162
163
164
# File 'app/controllers/sources_controller.rb', line 161

def set_source
  @source        = Source.find(params[:id])
  @recent_object = @source
end

- (Object) show

GET /sources/1 GET /sources/1.json



19
20
# File 'app/controllers/sources_controller.rb', line 19

def show
end

- (Object) source_params (private)



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'app/controllers/sources_controller.rb', line 169

def source_params
  params['source'][:project_sources_attributes] = [{project_id: sessions_current_project_id.to_s}]
  params.require(:source).permit(:serial_id, :address, :annote, :author, :booktitle, :chapter,
                                 :crossref, :edition, :editor, :howpublished, :institution,
                                 :journal, :key, :month, :note, :number, :organization, :pages,
                                 :publisher, :school, :series, :title, :type, :volume, :doi,
                                 :abstract, :copyright, :language, :stated_year, :verbatim,
                                 :bibtex_type, :day, :year, :isbn, :issn, :verbatim_contents,
                                 :verbatim_keywords, :language_id, :translator, :year_suffix, :url, :type,
                                 roles_attributes:           [:id,
                                                              :_destroy,
                                                              :type,
                                                              :person_id,
                                                              :position,
                                                              person_attributes:
                                                                [:last_name, :first_name, :suffix, :prefix]
                                                             ],
                                 project_sources_attributes: [:project_id]
  )
end

- (Object) update

PATCH/PUT /sources/1 PATCH/PUT /sources/1.json



71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/sources_controller.rb', line 71

def update
  respond_to do |format|
    if @source.update(source_params)
      format.html {redirect_to @source.metamorphosize, notice: 'Source was successfully updated.'}
      format.json {head :no_content}
    else
      format.html {render action: 'edit'}
      format.json {render json: @source.errors, status: :unprocessable_entity}
    end
  end
end