From f1a37ee97c5c946cf04a21ce9d01f6aed06dbef4 Mon Sep 17 00:00:00 2001 From: cejaramillof Date: Mon, 25 Jul 2016 18:04:48 -0500 Subject: [PATCH 1/2] Validations and view add vehicles --- Gemfile | 1 + Gemfile.lock | 4 ++ app/assets/javascripts/application.js | 2 +- app/controllers/vehicles_controller.rb | 12 +++--- app/models/location.rb | 4 ++ app/models/user.rb | 10 +++++ app/views/layouts/_user_menu.html.erb | 2 +- app/views/users/registrations/new.html.erb | 2 +- app/views/vehicles/new.html.erb | 22 ++++++++++ config/initializers/validates_timeliness.rb | 40 +++++++++++++++++++ config/locales/validates_timeliness.en.yml | 16 ++++++++ ...0_remove_date_details_from_park_records.rb | 5 +++ ...ry_date_exit_date_total_to_park_records.rb | 7 ++++ ...230124_add_max_slots_adress_to_location.rb | 6 +++ db/schema.rb | 10 +++-- 15 files changed, 131 insertions(+), 12 deletions(-) create mode 100644 app/views/vehicles/new.html.erb create mode 100644 config/initializers/validates_timeliness.rb create mode 100644 config/locales/validates_timeliness.en.yml create mode 100644 db/migrate/20160723010120_remove_date_details_from_park_records.rb create mode 100644 db/migrate/20160723011450_add_entry_date_exit_date_total_to_park_records.rb create mode 100644 db/migrate/20160725230124_add_max_slots_adress_to_location.rb diff --git a/Gemfile b/Gemfile index c7d0d62..d972ee0 100644 --- a/Gemfile +++ b/Gemfile @@ -24,6 +24,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' diff --git a/Gemfile.lock b/Gemfile.lock index ae7d527..107798c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -144,6 +144,7 @@ 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) @@ -151,6 +152,8 @@ GEM 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) @@ -176,6 +179,7 @@ DEPENDENCIES sqlite3 turbolinks uglifier (>= 1.3.0) + validates_timeliness (~> 4.0) web-console (~> 2.0) RUBY VERSION diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index c920e64..1019580 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -12,6 +12,6 @@ // //= require jquery //= require jquery_ujs -//= requiere bootstrap +//= require bootstrap //= require turbolinks //= require_tree . diff --git a/app/controllers/vehicles_controller.rb b/app/controllers/vehicles_controller.rb index b2f086e..2f8ce13 100644 --- a/app/controllers/vehicles_controller.rb +++ b/app/controllers/vehicles_controller.rb @@ -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 \ No newline at end of file diff --git a/app/models/location.rb b/app/models/location.rb index 38e5936..28fa6cb 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -1,3 +1,7 @@ class Location < ActiveRecord::Base has _many :slots + + validates :location_latitud, numericality: {only_float:true}, presence: true + validates :location_longitude, numericality: {only_float: true}, presence: true + end diff --git a/app/models/user.rb b/app/models/user.rb index 7b9c466..2e00dd3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 + + diff --git a/app/views/layouts/_user_menu.html.erb b/app/views/layouts/_user_menu.html.erb index 87efaaa..ed8d485 100644 --- a/app/views/layouts/_user_menu.html.erb +++ b/app/views/layouts/_user_menu.html.erb @@ -9,7 +9,7 @@
  • <%= link_to "Account" %>
  • <% if current_user.is_admin? %>
  • <%= link_to "Users", users_path%>
  • -
  • <%= link_to "Locations", locations_path%>
  • +
  • <%= link_to "Locations" %>
  • <%= link_to "Settings" %>
  • <%= link_to "History" %>
  • <% end %> diff --git a/app/views/users/registrations/new.html.erb b/app/views/users/registrations/new.html.erb index 4cb40c6..cd55a14 100644 --- a/app/views/users/registrations/new.html.erb +++ b/app/views/users/registrations/new.html.erb @@ -4,7 +4,7 @@ <%= devise_error_messages! %>
    <%= f.label :first_name %>
    - <%= f.text_field :first_name, autofocus: true %> + <%= f.text_field :first_name, placeholder: "Nombre", autofocus: true %>
    diff --git a/app/views/vehicles/new.html.erb b/app/views/vehicles/new.html.erb new file mode 100644 index 0000000..7f508b6 --- /dev/null +++ b/app/views/vehicles/new.html.erb @@ -0,0 +1,22 @@ +

    Register new vehicle

    +

    New Vehicle for <%= @user.first_name %> <%= @user.last_name %>

    +<%= form_for @vehicle, url: {action: 'create'} do |form|%> +
    + <%= form.label :model, for:'model' %>
    + <%= form.text_field :model, placeholder: "Modelo", autofocus: true, required: true %> +
    + +
    + <%= form.label :year %>
    + <%= form.select :year, (Time.zone.now.year - 100)..(Time.zone.now.year), required: true %> +
    + +
    + <%= form.label :vin %>
    + <%= form.text_field :vin, placeholder: "Placa", autofocus: true, required: true %> +
    + +
    + <%= form.submit "Register Vehicle" %><%= link_to "Cancel", user_vehicles_path(@user) %> +
    +<% end %> \ No newline at end of file diff --git a/config/initializers/validates_timeliness.rb b/config/initializers/validates_timeliness.rb new file mode 100644 index 0000000..9f004af --- /dev/null +++ b/config/initializers/validates_timeliness.rb @@ -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 diff --git a/config/locales/validates_timeliness.en.yml b/config/locales/validates_timeliness.en.yml new file mode 100644 index 0000000..634c9da --- /dev/null +++ b/config/locales/validates_timeliness.en.yml @@ -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' diff --git a/db/migrate/20160723010120_remove_date_details_from_park_records.rb b/db/migrate/20160723010120_remove_date_details_from_park_records.rb new file mode 100644 index 0000000..ec5ed77 --- /dev/null +++ b/db/migrate/20160723010120_remove_date_details_from_park_records.rb @@ -0,0 +1,5 @@ +class RemoveDateDetailsFromParkRecords < ActiveRecord::Migration + def change + remove_column :park_records, :date_details, :datetime + end +end diff --git a/db/migrate/20160723011450_add_entry_date_exit_date_total_to_park_records.rb b/db/migrate/20160723011450_add_entry_date_exit_date_total_to_park_records.rb new file mode 100644 index 0000000..310d550 --- /dev/null +++ b/db/migrate/20160723011450_add_entry_date_exit_date_total_to_park_records.rb @@ -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 diff --git a/db/migrate/20160725230124_add_max_slots_adress_to_location.rb b/db/migrate/20160725230124_add_max_slots_adress_to_location.rb new file mode 100644 index 0000000..5e852c5 --- /dev/null +++ b/db/migrate/20160725230124_add_max_slots_adress_to_location.rb @@ -0,0 +1,6 @@ +class AddMaxSlotsAdressToLocation < ActiveRecord::Migration + def change + add_column :locations, :max_slots, :integer + add_column :locations, :adress, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 7da8c7e..2d96d80 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160716010437) do +ActiveRecord::Schema.define(version: 20160723011450) do create_table "locations", force: :cascade do |t| t.float "location_latitude" @@ -22,12 +22,14 @@ 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" From ef9c285f5b92735d42cae479ca4faf5f7a3c59a2 Mon Sep 17 00:00:00 2001 From: cejaramillof Date: Mon, 25 Jul 2016 19:28:23 -0500 Subject: [PATCH 2/2] New validations --- Gemfile | 8 ++++++-- Gemfile.lock | 8 ++++++++ app/models/location.rb | 4 ++-- app/models/park_record.rb | 3 +++ app/models/vehicle.rb | 5 +++++ .../20160725232754_add_index_to_users_email.rb | 4 ++++ ...ename_locations_location_latitudeto_latitude.rb | 5 +++++ ...ame_locations_location_longitudeto_longitude.rb | 5 +++++ db/schema.rb | 14 ++++++++------ 9 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 db/migrate/20160725232754_add_index_to_users_email.rb create mode 100644 db/migrate/20160725235348_rename_locations_location_latitudeto_latitude.rb create mode 100644 db/migrate/20160725235447_rename_locations_location_longitudeto_longitude.rb diff --git a/Gemfile b/Gemfile index d972ee0..275fb47 100644 --- a/Gemfile +++ b/Gemfile @@ -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 @@ -35,6 +33,8 @@ gem 'validates_timeliness', '~> 4.0' # 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 @@ -47,3 +47,7 @@ group :development do gem 'spring' end +group :production do + gem 'pg', '0.17.1' + gem 'rails_12factor', '0.0.2' +end \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 107798c..a9b4837 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -172,7 +178,9 @@ 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 diff --git a/app/models/location.rb b/app/models/location.rb index 28fa6cb..4e27c9f 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -1,7 +1,7 @@ class Location < ActiveRecord::Base has _many :slots - validates :location_latitud, numericality: {only_float:true}, presence: true - validates :location_longitude, numericality: {only_float: true}, presence: true + validates :latitude, numericality: {only_float:true}, presence: true + validates :longitude, numericality: {only_float: true}, presence: true end diff --git a/app/models/park_record.rb b/app/models/park_record.rb index 8ff1b6d..0ac1ac2 100644 --- a/app/models/park_record.rb +++ b/app/models/park_record.rb @@ -1,4 +1,7 @@ class ParkRecord < ActiveRecord::Base belongs_to :slot belongs_to :vehicle + + validates :entry_date, presence: true + end diff --git a/app/models/vehicle.rb b/app/models/vehicle.rb index 9edb986..27336b3 100644 --- a/app/models/vehicle.rb +++ b/app/models/vehicle.rb @@ -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 diff --git a/db/migrate/20160725232754_add_index_to_users_email.rb b/db/migrate/20160725232754_add_index_to_users_email.rb new file mode 100644 index 0000000..b5f8a1a --- /dev/null +++ b/db/migrate/20160725232754_add_index_to_users_email.rb @@ -0,0 +1,4 @@ +class AddIndexToUsersEmail < ActiveRecord::Migration + def change + end +end diff --git a/db/migrate/20160725235348_rename_locations_location_latitudeto_latitude.rb b/db/migrate/20160725235348_rename_locations_location_latitudeto_latitude.rb new file mode 100644 index 0000000..6a91624 --- /dev/null +++ b/db/migrate/20160725235348_rename_locations_location_latitudeto_latitude.rb @@ -0,0 +1,5 @@ +class RenameLocationsLocationLatitudetoLatitude < ActiveRecord::Migration + def change + rename_column :Locations, :location_latitude, :latitude + end +end diff --git a/db/migrate/20160725235447_rename_locations_location_longitudeto_longitude.rb b/db/migrate/20160725235447_rename_locations_location_longitudeto_longitude.rb new file mode 100644 index 0000000..ea56de9 --- /dev/null +++ b/db/migrate/20160725235447_rename_locations_location_longitudeto_longitude.rb @@ -0,0 +1,5 @@ +class RenameLocationsLocationLongitudetoLongitude < ActiveRecord::Migration + def change + rename_column :Locations, :location_longitude, :longitude + end +end diff --git a/db/schema.rb b/db/schema.rb index 2d96d80..742a13f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,14 +11,16 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160723011450) 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|