diff --git a/Gemfile b/Gemfile index 9dd0452..dded4ed 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,6 @@ source 'https://rubygems.org' +gem 'paperclip', '~> 5.0.0' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.0.0', '>= 5.0.0.1' diff --git a/Gemfile.lock b/Gemfile.lock index ff0ff7b..e3e0110 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -42,6 +42,10 @@ GEM bcrypt (3.1.11) builder (3.2.2) byebug (9.0.6) + climate_control (0.0.3) + activesupport (>= 3.0) + cocaine (0.5.8) + climate_control (>= 0.0.3, < 1.0) coffee-rails (4.2.1) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.2.x) @@ -75,12 +79,19 @@ GEM mime-types (3.1) mime-types-data (~> 3.2015) mime-types-data (3.2016.0521) + mimemagic (0.3.2) mini_portile2 (2.1.0) minitest (5.9.1) multi_json (1.12.1) nio4r (1.2.1) nokogiri (1.6.8.1) mini_portile2 (~> 2.1.0) + paperclip (5.0.0) + activemodel (>= 4.2.0) + activesupport (>= 4.2.0) + cocaine (~> 0.5.5) + mime-types + mimemagic (~> 0.3.0) puma (3.6.2) rack (2.0.1) rack-test (0.6.3) @@ -161,6 +172,7 @@ DEPENDENCIES jbuilder (~> 2.5) jquery-rails listen (~> 3.0.5) + paperclip (~> 5.0.0) puma (~> 3.0) rails (~> 5.0.0, >= 5.0.0.1) sass-rails (~> 5.0) diff --git a/app/assets/javascripts/documents.coffee b/app/assets/javascripts/documents.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/documents.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/documents.scss b/app/assets/stylesheets/documents.scss new file mode 100644 index 0000000..ec6aac2 --- /dev/null +++ b/app/assets/stylesheets/documents.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Documents controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/documents_controller.rb b/app/controllers/documents_controller.rb new file mode 100644 index 0000000..9314e16 --- /dev/null +++ b/app/controllers/documents_controller.rb @@ -0,0 +1,74 @@ +class DocumentsController < ApplicationController + before_action :set_document, only: [:show, :edit, :update, :destroy] + + # GET /documents + # GET /documents.json + def index + @documents = Document.all + end + + # GET /documents/1 + # GET /documents/1.json + def show + end + + # GET /documents/new + def new + @document = Document.new + end + + # GET /documents/1/edit + def edit + end + + # POST /documents + # POST /documents.json + def create + @document = Document.new(document_params) + + respond_to do |format| + if @document.save + format.html { redirect_to @document, notice: 'Document was successfully created.' } + format.json { render :show, status: :created, location: @document } + else + format.html { render :new } + format.json { render json: @document.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /documents/1 + # PATCH/PUT /documents/1.json + def update + respond_to do |format| + if @document.update(document_params) + format.html { redirect_to @document, notice: 'Document was successfully updated.' } + format.json { render :show, status: :ok, location: @document } + else + format.html { render :edit } + format.json { render json: @document.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /documents/1 + # DELETE /documents/1.json + def destroy + @document.destroy + respond_to do |format| + format.html { redirect_to documents_url, notice: 'Document was successfully destroyed.' } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_document + @document = Document.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def document_params + params.require(:document).permit(:name, :doc) + end +end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 7383491..e941571 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -8,7 +8,7 @@ class SessionsController < ApplicationController user = User.find_by(name: params[:name]) if user&.authenticate(params[:password]) session[:user_id] = user.id - redirect_to admin_url + redirect_to documents_url else redirect_to login_url end diff --git a/app/helpers/documents_helper.rb b/app/helpers/documents_helper.rb new file mode 100644 index 0000000..242b4fc --- /dev/null +++ b/app/helpers/documents_helper.rb @@ -0,0 +1,2 @@ +module DocumentsHelper +end diff --git a/app/models/document.rb b/app/models/document.rb new file mode 100644 index 0000000..a39a400 --- /dev/null +++ b/app/models/document.rb @@ -0,0 +1,4 @@ +class Document < ApplicationRecord + has_attached_file :doc + validates_attachment_content_type :doc, content_type: 'application/pdf' +end diff --git a/app/views/documents/_document.json.jbuilder b/app/views/documents/_document.json.jbuilder new file mode 100644 index 0000000..2e9c923 --- /dev/null +++ b/app/views/documents/_document.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! document, :id, :name, :created_at, :updated_at +json.url document_url(document, format: :json) \ No newline at end of file diff --git a/app/views/documents/_form.html.erb b/app/views/documents/_form.html.erb new file mode 100644 index 0000000..cacf85b --- /dev/null +++ b/app/views/documents/_form.html.erb @@ -0,0 +1,27 @@ +<%= form_for document, url: documents_path, html: { multipart: true } do |f| %> + <% if document.errors.any? %> +
<%= notice %>
+ +| Name | ++ | ||
|---|---|---|---|
| <%= document.name %> | +<%= link_to 'Show', document %> | +<%= link_to 'Edit', edit_document_path(document) %> | +<%= link_to 'Destroy', document, method: :delete, data: { confirm: 'Are you sure?' } %> | +
<%= notice %>
+ ++ Name: + <%= @document.name %> +
+ ++ <%= link_to @document.doc_file_name, @document.doc.url %> +
+ +<%= link_to 'Edit', edit_document_path(@document) %> | +<%= link_to 'Back', documents_path %> diff --git a/app/views/documents/show.json.jbuilder b/app/views/documents/show.json.jbuilder new file mode 100644 index 0000000..87dd6eb --- /dev/null +++ b/app/views/documents/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "documents/document", document: @document \ No newline at end of file diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 3738d56..7c5b603 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -9,6 +9,15 @@ - <%= yield %> +