Skip to content

Commit

Permalink
show validation message
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkasyanchuk committed Aug 28, 2018
1 parent 8f5c8e4 commit 4bffc71
Show file tree
Hide file tree
Showing 21 changed files with 219 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -14,6 +14,6 @@ gemspec
# To use a debugger
# gem 'byebug', group: [:development, :test]

group :test do
group :development, :test do
gem 'pry'
end
4 changes: 2 additions & 2 deletions Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
active_storage_validations (0.1)
active_storage_validations (0.3)
rails (~> 5.2.0)

GEM
Expand Down Expand Up @@ -129,4 +129,4 @@ DEPENDENCIES
sqlite3

BUNDLED WITH
1.13.6
1.16.1
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -20,6 +20,20 @@ class User < ApplicationRecord
end
```

or

```ruby
class Project < ApplicationRecord
has_one_attached :preview
has_one_attached :attachment

validates :title, presence: true

validates :preview, attached: true
validates :attachment, attached: true, content_type: { in: 'application/pdf', message: 'is not a PDF' }
end
```

## Installation

Add this line to your application's Gemfile:
Expand Down
Binary file added docs/preview.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion lib/active_storage_validations.rb
Expand Up @@ -11,6 +11,11 @@ def validate_each(record, attribute, value)
end

class ContentTypeValidator < ActiveModel::EachValidator

# def initialize(options)
# super(options)
# end

def validate_each(record, attribute, value)
files = record.send(attribute)

Expand All @@ -24,7 +29,7 @@ def validate_each(record, attribute, value)

files.each do |file|
unless content_type_valid?(file)
record.errors.add(attribute, :invalid)
record.errors.add(attribute, options[:message].presence || :invalid)
return
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_storage_validations/version.rb
@@ -1,3 +1,3 @@
module ActiveStorageValidations
VERSION = '0.2'
VERSION = '0.3'
end
1 change: 0 additions & 1 deletion test/dummy/.ruby-version

This file was deleted.

2 changes: 2 additions & 0 deletions test/dummy/app/assets/javascripts/projects.js
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
4 changes: 4 additions & 0 deletions test/dummy/app/assets/stylesheets/projects.css
@@ -0,0 +1,4 @@
/*
Place all the styles related to the matching controller here.
They will automatically be included in application.css.
*/
58 changes: 58 additions & 0 deletions test/dummy/app/controllers/projects_controller.rb
@@ -0,0 +1,58 @@
class ProjectsController < ApplicationController
before_action :set_project, only: [:show, :edit, :update, :destroy]

# GET /projects
def index
@projects = Project.all
end

# GET /projects/1
def show
end

# GET /projects/new
def new
@project = Project.new
end

# GET /projects/1/edit
def edit
end

# POST /projects
def create
@project = Project.new(project_params)

if @project.save
redirect_to @project, notice: 'Project was successfully created.'
else
render :new
end
end

# PATCH/PUT /projects/1
def update
if @project.update(project_params)
redirect_to @project, notice: 'Project was successfully updated.'
else
render :edit
end
end

# DELETE /projects/1
def destroy
@project.destroy
redirect_to projects_url, notice: 'Project was successfully destroyed.'
end

private
# Use callbacks to share common setup or constraints between actions.
def set_project
@project = Project.find(params[:id])
end

# Only allow a trusted parameter "white list" through.
def project_params
params.require(:project).permit!
end
end
2 changes: 2 additions & 0 deletions test/dummy/app/helpers/projects_helper.rb
@@ -0,0 +1,2 @@
module ProjectsHelper
end
19 changes: 19 additions & 0 deletions test/dummy/app/models/project.rb
@@ -0,0 +1,19 @@
# == Schema Information
#
# Table name: projects
#
# id :integer not null, primary key
# title :string
# created_at :datetime not null
# updated_at :datetime not null
#

class Project < ApplicationRecord
has_one_attached :preview
has_one_attached :attachment

validates :title, presence: true

validates :preview, attached: true
validates :attachment, attached: true, content_type: { in: 'application/pdf', message: 'is not a PDF' }
end
4 changes: 4 additions & 0 deletions test/dummy/app/views/layouts/application.html.erb 100644 → 100755
Expand Up @@ -10,6 +10,10 @@
</head>

<body>
<ul>
<li><%= link_to 'Users', users_path %></li>
<li><%= link_to 'Projects', projects_path %></li>
</ul>
<%= yield %>
</body>
</html>
32 changes: 32 additions & 0 deletions test/dummy/app/views/projects/_form.html.erb
@@ -0,0 +1,32 @@
<%= form_with(model: project, local: true) do |form| %>
<% if project.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(project.errors.count, "error") %> prohibited this project from being saved:</h2>

<ul>
<% project.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= form.label :title %>
<%= form.text_field :title %>
</div>

<div class="field">
<%= form.label :preview %>
<%= form.file_field :preview %>
</div>

<div class="field">
<%= form.label :attachment %>
<%= form.file_field :attachment %>
</div>

<div class="actions">
<%= form.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions test/dummy/app/views/projects/edit.html.erb
@@ -0,0 +1,6 @@
<h1>Editing Project</h1>

<%= render 'form', project: @project %>
<%= link_to 'Show', @project %> |
<%= link_to 'Back', projects_path %>
27 changes: 27 additions & 0 deletions test/dummy/app/views/projects/index.html.erb
@@ -0,0 +1,27 @@
<p id="notice"><%= notice %></p>

<h1>Projects</h1>

<table>
<thead>
<tr>
<th>Title</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @projects.each do |project| %>
<tr>
<td><%= project.title %></td>
<td><%= link_to 'Show', project %></td>
<td><%= link_to 'Edit', edit_project_path(project) %></td>
<td><%= link_to 'Destroy', project, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Project', new_project_path %>
5 changes: 5 additions & 0 deletions test/dummy/app/views/projects/new.html.erb
@@ -0,0 +1,5 @@
<h1>New Project</h1>

<%= render 'form', project: @project %>
<%= link_to 'Back', projects_path %>
19 changes: 19 additions & 0 deletions test/dummy/app/views/projects/show.html.erb
@@ -0,0 +1,19 @@
<p id="notice"><%= notice %></p>

<p>
<strong>Title:</strong>
<%= @project.title %>
</p>

<p>
<strong>Preview:</strong>
<%= link_to 'download', @project.preview %>
</p>

<p>
<strong>Attachment:</strong>
<%= link_to 'download', @project.attachment %>
</p>

<%= link_to 'Edit', edit_project_path(@project) %> |
<%= link_to 'Back', projects_path %>
1 change: 1 addition & 0 deletions test/dummy/config/routes.rb
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :projects
resources :users
root 'users#index'
end
9 changes: 9 additions & 0 deletions test/dummy/db/migrate/20180828190302_create_projects.rb
@@ -0,0 +1,9 @@
class CreateProjects < ActiveRecord::Migration[5.2]
def change
create_table :projects do |t|
t.string :title

t.timestamps
end
end
end
8 changes: 7 additions & 1 deletion test/dummy/db/schema.rb
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_08_28_145143) do
ActiveRecord::Schema.define(version: 2018_08_28_190302) do

create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
Expand All @@ -33,6 +33,12 @@
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end

create_table "projects", force: :cascade do |t|
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "users", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
Expand Down

0 comments on commit 4bffc71

Please sign in to comment.