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



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

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



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

def batch_load
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
68
# 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



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/controllers/sources_controller.rb', line 146

def create_bibtex_batch_load
  if params[:file].nil?
    redirect_to batch_load_sources_path, notice: 'no file has been selected'
  else
    sha256 = Digest::SHA256.file(params[:file].tempfile)
    if cookies[:batch_sources_md5] == sha256.hexdigest
      if result_hash = Source.batch_create(params.symbolize_keys.to_h)
        @count         = result_hash[:count]
        @sources       = result_hash[:records]
        flash[:notice] = "Successfully batch created #{@count} sources."
        render 'sources/batch_load/bibtex_batch_create' # and return
      else
        flash[:notice] = 'Failed to create the 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
#      redirect_to sources_path
  end
end

- (Object) destroy

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



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

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



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

def download
  send_data Source.generate_download(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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/controllers/sources_controller.rb', line 120

def preview_bibtex_batch_load
  file = params[:file]
  if file.blank?
    redirect_to batch_load_sources_path, notice: 'No file has been selected.'
  else
    begin # file type testing: Permit UTF-8, ASCII
      failed   = false
      mimetype = `file -b "#{file.tempfile.path}"`.gsub(/\n/, '')
      case mimetype
        when /utf-8/i, /ascii/i
          failed = false # redundant, but good for debugging
        else
          failed = true
      end
      if failed
        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: file.tempfile)
        sha256                      = Digest::SHA256.file(file.tempfile)
        cookies[:batch_sources_md5] = sha256.hexdigest
        render 'sources/batch_load/bibtex_batch_preview'
      end
    end
  end
end

- (Object) search



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

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)



176
177
178
179
# File 'app/controllers/sources_controller.rb', line 176

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)



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 181

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



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

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