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, #whitelist_constantize

Methods included from ProjectsHelper

#project_link, #project_tag, #projects_list, #projects_search_form

Instance Method Details

- (Object) autocomplete



103
104
105
106
107
108
# File 'app/controllers/sources_controller.rb', line 103

def autocomplete
  @sources = Queries::Source::Autocomplete.new(
    params.require(:term),
    autocomplete_params
  ).autocomplete
end

- (Object) autocomplete_params



110
111
112
# File 'app/controllers/sources_controller.rb', line 110

def autocomplete_params
  params.permit(:limit_to_project).merge(project_id: sessions_current_project_id).to_h.symbolize_keys
end

- (Object) batch_load

GET /sources/batch_load This is deprecated



124
125
# File 'app/controllers/sources_controller.rb', line 124

def batch_load
end

- (Object) batch_params (private)



175
176
# File 'app/controllers/sources_controller.rb', line 175

def batch_params
end

- (Object) create

POST /sources POST /sources.json TODO: move all the logic to model(s) rubocop:disable Metrics/BlockNesting TODO: check on this



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
68
69
70
71
72
73
74
75
# File 'app/controllers/sources_controller.rb', line 35

def create
  @source = Source.new(source_params)

  respond_to do |format|
    if @source.save
      case @source.type
        when 'Source::Bibtex'
          format.html { redirect_to url_for(@source.metamorphosize),
                                    notice: "Source by '#{@source.author}' was successfully created." }
        when 'Source::Verbatim'
          format.html { redirect_to url_for(@source.metamorphosize),
                                    notice: "Source '#{@source.cached}' was successfully created." }
        else # type human
          format.html { redirect_to url_for(@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 url_for(@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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/controllers/sources_controller.rb', line 142

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



95
96
97
98
99
100
101
# File 'app/controllers/sources_controller.rb', line 95

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



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

def download
  send_data Download.generate_csv(Source.all), type: 'text', filename: "sources_#{DateTime.now}.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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/controllers/sources_controller.rb', line 127

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



114
115
116
117
118
119
120
121
# File 'app/controllers/sources_controller.rb', line 114

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)



170
171
172
173
# File 'app/controllers/sources_controller.rb', line 170

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)



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'app/controllers/sources_controller.rb', line 178

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



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

def update
  respond_to do |format|
    if @source.update(source_params)
      format.html { redirect_to url_for(@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