Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validations form #2

Merged
merged 2 commits into from
Jul 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ source 'https://rubygems.org'
ruby '2.3.1'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.6'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
Expand All @@ -24,6 +22,7 @@ gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'devise'
gem 'bootstrap-sass'
gem 'validates_timeliness', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

Expand All @@ -34,6 +33,8 @@ gem 'bootstrap-sass'
# gem 'capistrano-rails', group: :development

group :development, :test do
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
end
Expand All @@ -46,3 +47,7 @@ group :development do
gem 'spring'
end

group :production do
gem 'pg', '0.17.1'
gem 'rails_12factor', '0.0.2'
end
12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ GEM
mini_portile2 (~> 2.1.0)
pkg-config (~> 1.1.7)
orm_adapter (0.5.0)
pg (0.17.1)
pkg-config (1.1.7)
rack (1.6.4)
rack-test (0.6.3)
Expand All @@ -112,6 +113,11 @@ GEM
rails-deprecated_sanitizer (>= 1.0.1)
rails-html-sanitizer (1.0.3)
loofah (~> 2.0)
rails_12factor (0.0.2)
rails_serve_static_assets
rails_stdout_logging
rails_serve_static_assets (0.0.5)
rails_stdout_logging (0.0.5)
railties (4.2.6)
actionpack (= 4.2.6)
activesupport (= 4.2.6)
Expand Down Expand Up @@ -144,13 +150,16 @@ GEM
thor (0.19.1)
thread_safe (0.3.5)
tilt (2.0.5)
timeliness (0.3.8)
turbolinks (5.0.0)
turbolinks-source (~> 5)
turbolinks-source (5.0.0)
tzinfo (1.2.2)
thread_safe (~> 0.1)
uglifier (3.0.0)
execjs (>= 0.3.0, < 3)
validates_timeliness (4.0.2)
timeliness (~> 0.3.7)
warden (1.2.6)
rack (>= 1.0)
web-console (2.3.0)
Expand All @@ -169,13 +178,16 @@ DEPENDENCIES
devise
jbuilder (~> 2.0)
jquery-rails
pg (= 0.17.1)
rails (= 4.2.6)
rails_12factor (= 0.0.2)
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
spring
sqlite3
turbolinks
uglifier (>= 1.3.0)
validates_timeliness (~> 4.0)
web-console (~> 2.0)

RUBY VERSION
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
//
//= require jquery
//= require jquery_ujs
//= requiere bootstrap
//= require bootstrap
//= require turbolinks
//= require_tree .
12 changes: 7 additions & 5 deletions app/controllers/vehicles_controller.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
class VehiclessController < ApplicationController
class VehiclesController < ApplicationController
def new
@user = current_user
@vechicle = Vehicle.new
@vehicle = Vehicle.new
end

def create
@vehicle = Vehicle.new(vehicle_params)
@vehicle.user_id = current_user.id
if @vehicle.save
flash[:success] = 'Vehicle registered successfully.' redirect_to user vehicles_path(current_user)
flash[:success] = 'Vehicle registered successfully.'
redirect_to user_vehicles_path(current_user)
else
flash[:error] = @vehicle.errors.full_messages.join('.') render 'new'
flash[:error] = @vehicle.errors.full_messages.join('.')
render 'new'
end
end

def vehicle_params
params.require(:vehicle).permit(:user_id, :model, :year, :vim, :id)
params.require(:vehicle).permit(:user_id, :model, :year, :vin, :id)
end
private :vehicle_params
end
4 changes: 4 additions & 0 deletions app/models/location.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
class Location < ActiveRecord::Base
has _many :slots

validates :latitude, numericality: {only_float:true}, presence: true
validates :longitude, numericality: {only_float: true}, presence: true

end
3 changes: 3 additions & 0 deletions app/models/park_record.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class ParkRecord < ActiveRecord::Base
belongs_to :slot
belongs_to :vehicle

validates :entry_date, presence: true

end
10 changes: 10 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,14 @@ class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :vehicles

validates :first_name, presence: true, format: { with: /\A[a-zA-Z]+\z/, message: "only allows letters" }
validates :last_name, presence: true, format: { with: /\A[a-zA-Z]+\z/, message: "only allows letters" }
validates :email, presence: true, uniqueness: true, format: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, length: { maximum: 254 }
validates :password, presence: true, confirmation: true
validates :password_confirmation, presence: true
validates :birthdate, presence: true, :timeliness => {:on_or_before => lambda { Date.current }, :type => :date}
# validates :username, format: { with: /regex/ }
end


5 changes: 5 additions & 0 deletions app/models/vehicle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ class Vehicle < ActiveRecord::Base
belongs_to :user
has_many :park_records
has_many :slots, through: :park_records

validates :model, presence: true
validates :year, presence: true
validates :vin, presence: true

end
2 changes: 1 addition & 1 deletion app/views/layouts/_user_menu.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<li><%= link_to "Account" %></li>
<% if current_user.is_admin? %>
<li><%= link_to "Users", users_path%></li>
<li><%= link_to "Locations", locations_path%></li>
<li><%= link_to "Locations" %></li>
<li><%= link_to "Settings" %></li>
<li><%= link_to "History" %></li>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/registrations/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<%= devise_error_messages! %>
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name, autofocus: true %>
<%= f.text_field :first_name, placeholder: "Nombre", autofocus: true %>
</div>

<div class="field">
Expand Down
22 changes: 22 additions & 0 deletions app/views/vehicles/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<h2>Register new vehicle</h2>
<h3>New Vehicle for <%= @user.first_name %> <%= @user.last_name %></h3>
<%= form_for @vehicle, url: {action: 'create'} do |form|%>
<div class="field">
<%= form.label :model, for:'model' %><br />
<%= form.text_field :model, placeholder: "Modelo", autofocus: true, required: true %>
</div>

<div class="field">
<%= form.label :year %><br />
<%= form.select :year, (Time.zone.now.year - 100)..(Time.zone.now.year), required: true %>
</div>

<div class="field">
<%= form.label :vin %><br />
<%= form.text_field :vin, placeholder: "Placa", autofocus: true, required: true %>
</div>

<div class="actions">
<%= form.submit "Register Vehicle" %><%= link_to "Cancel", user_vehicles_path(@user) %>
</div>
<% end %>
40 changes: 40 additions & 0 deletions config/initializers/validates_timeliness.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
ValidatesTimeliness.setup do |config|
# Extend ORM/ODMs for full support (:active_record included).
config.extend_orms = [ :active_record ]
#
# Default timezone
# config.default_timezone = :utc
#
# Set the dummy date part for a time type values.
# config.dummy_date_for_time_type = [ 2000, 1, 1 ]
#
# Ignore errors when restriction options are evaluated
# config.ignore_restriction_errors = false
#
# Re-display invalid values in date/time selects
# config.enable_date_time_select_extension!
#
# Handle multiparameter date/time values strictly
# config.enable_multiparameter_extension!
#
# Shorthand date and time symbols for restrictions
# config.restriction_shorthand_symbols.update(
# :now => lambda { Time.current },
# :today => lambda { Date.current }
# )
#
# Use the plugin date/time parser which is stricter and extendable
# config.use_plugin_parser = false
#
# Add one or more formats making them valid. e.g. add_formats(:date, 'd(st|rd|th) of mmm, yyyy')
# config.parser.add_formats()
#
# Remove one or more formats making them invalid. e.g. remove_formats(:date, 'dd/mm/yyy')
# config.parser.remove_formats()
#
# Change the ambiguous year threshold when parsing a 2 digit year
# config.parser.ambiguous_year_threshold = 30
#
# Treat ambiguous dates, such as 01/02/1950, as a Non-US date.
# config.parser.remove_us_formats
end
16 changes: 16 additions & 0 deletions config/locales/validates_timeliness.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
en:
errors:
messages:
invalid_date: "is not a valid date"
invalid_time: "is not a valid time"
invalid_datetime: "is not a valid datetime"
is_at: "must be at %{restriction}"
before: "must be before %{restriction}"
on_or_before: "must be on or before %{restriction}"
after: "must be after %{restriction}"
on_or_after: "must be on or after %{restriction}"
validates_timeliness:
error_value_formats:
date: '%Y-%m-%d'
time: '%H:%M:%S'
datetime: '%Y-%m-%d %H:%M:%S'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveDateDetailsFromParkRecords < ActiveRecord::Migration
def change
remove_column :park_records, :date_details, :datetime
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddEntryDateExitDateTotalToParkRecords < ActiveRecord::Migration
def change
add_column :park_records, :entry_date, :datetime
add_column :park_records, :exit_date, :datetime
add_column :park_records, :total, :float
end
end
6 changes: 6 additions & 0 deletions db/migrate/20160725230124_add_max_slots_adress_to_location.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddMaxSlotsAdressToLocation < ActiveRecord::Migration
def change
add_column :locations, :max_slots, :integer
add_column :locations, :adress, :string
end
end
4 changes: 4 additions & 0 deletions db/migrate/20160725232754_add_index_to_users_email.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class AddIndexToUsersEmail < ActiveRecord::Migration
def change
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RenameLocationsLocationLatitudetoLatitude < ActiveRecord::Migration
def change
rename_column :Locations, :location_latitude, :latitude
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RenameLocationsLocationLongitudetoLongitude < ActiveRecord::Migration
def change
rename_column :Locations, :location_longitude, :longitude
end
end
22 changes: 13 additions & 9 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,27 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160716010437) do
ActiveRecord::Schema.define(version: 20160725235447) do

create_table "locations", force: :cascade do |t|
t.float "location_latitude"
t.float "location_longitude"
create_table "Locations", force: :cascade do |t|
t.float "latitude"
t.float "longitude"
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "max_slots"
t.string "adress"
end

create_table "park_records", force: :cascade do |t|
t.datetime "date_details"
t.string "comments"
t.integer "slot_id"
t.integer "vehicle_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "entry_date"
t.datetime "exit_date"
t.float "total"
end

add_index "park_records", ["slot_id"], name: "index_park_records_on_slot_id"
Expand Down