Comments (4)
Can you tell what version of Rails you are using? Also, please post a gist with the example model and tests, if you can.
from validates_timeliness.
Thanks for the follow up! Using rails 3.0.9, ruby 1.9.2, validates_timeliness 3.0.2 (full set of gems listed below)
Example Model
class Event ActiveRecord::Base attr_accessible :event_name, :seats, :event_date, :cancel_date, :rsvp_date, :remaining_seats belongs_to :user validates :event_name, :presence => true, :length => { :maximum => 100 } validates :seats, :numericality => { :only_integer => true }, :inclusion => { :in => 1..30 } validates :user_id, :presence => true # validate dates - using validates_timeliness gem validates_datetime :cancel_date, :on_or_before => :event_date validates_datetime :event_date, :on_or_after => lambda {Date.current} validates_datetime :rsvp_date, :on_or_after => lambda {Date.current}, :on_or_before => :event_date
Example Controller
class EventsController ApplicationController before_filter :authenticate before_filter :authorized_host, :only => [:edit, :update] before_filter :admin_user, :only => [:destroy, :index] def new @event = Event.new @title = "New Event" end def create @event = current_user.events.build(params[:event]) if @event.save flash[:success] = "Event created!" redirect_to @event else render 'new' end end def show @event = Event.find(params[:id]) @host = User.find_by_id(@event.user_id) @title = "Event: #{@event.event_name}" end def edit @title = "Edit Event" @event = Event.find(params[:id]) end def update @event = Event.find(params[:id]) if @event.update_attributes(params[:event]) flash[:success] = "Event updated." redirect_to current_user else @title = "Edit Event" render 'edit' end end def index @title = "All events" @events = Event.paginate(:page => params[:page]) end def destroy @event = Event.find(params[:id]) if @event.destroy flash[:success] = "Event deleted" # later want to render index again or something # more useful to the admin redirect_to current_user end end private def authorized_host @event = current_user.events.find_by_id(params[:id]) if @event.nil? flash[:notice] = "You must be the host of the event in order to edit it." redirect_to(current_user) end end def admin_user redirect_to(current_user) unless current_user.admin? end end
Tests
The validate dates tests work if I uncomment the validates_datetime lines in the model. Additionally, if I have those uncommented and create a new Event object in the console, it will give me the appropriate error message if I give an invalid date, indicating the validates_datetime seems to be working in that environment as well.
require 'spec_helper' describe Event do before(:each) do @user = Factory(:user) @attr = { :event_name => "Test Name", :seats => 3, :event_date => Time.now + 24*60*60*2, :rsvp_date => Time.now + 24*60*60*2, :cancel_date => Time.now - 24*60*60} end it "should create a new event instance given valid attributes" do @user.events.create!(@attr) end describe "validate dates" do it "should require valid datetime event_date" do @user.events.build(@attr.merge(:event_date => ' ')). should_not be_valid @user.events.build(@attr.merge(:event_date => 1.years.ago)). should_not be_valid end
gem list
Using rake (0.8.7)
Using abstract (1.0.0)
Using activesupport (3.0.9)
Using builder (2.1.2)
Using i18n (0.5.0)
Using activemodel (3.0.9)
Using erubis (2.6.6)
Using rack (1.2.3)
Using rack-mount (0.6.14)
Using rack-test (0.5.7)
Using tzinfo (0.3.29)
Using actionpack (3.0.9)
Using mime-types (1.16)
Using polyglot (0.3.2)
Using treetop (1.4.10)
Using mail (2.2.19)
Using actionmailer (3.0.9)
Using arel (2.0.10)
Using activerecord (3.0.9)
Using activeresource (3.0.9)
Using annotate (2.4.0)
Using bundler (1.0.15)
Using diff-lcs (1.1.2)
Using factory_girl (1.3.3)
Using rdoc (3.9.2)
Using thor (0.14.6)
Using railties (3.0.9)
Using rails (3.0.9)
Using factory_girl_rails (1.0)
Using faker (0.3.1)
Using gravatar_image_tag (0.1.0)
Using money (3.7.1)
Using nokogiri (1.5.0)
Using rspec-core (2.0.1)
Using rspec-expectations (2.0.1)
Using rspec-mocks (2.0.1)
Using rspec (2.0.1)
Using rspec-rails (2.0.1)
Using sqlite3 (1.3.4)
Using timeliness (0.3.4)
Using validates_timeliness (3.0.2)
Using webrat (0.7.1)
Using will_paginate (3.0.pre2)
from validates_timeliness.
Nothing seems obviously wrong. Though you are on an old version of the plugin. Try updating to 3.0.6 first and let me know.
As a tip, instead things like '24_60_60*2', you can use Time.now + 2.days.
from validates_timeliness.
Did you ever figure this out?
from validates_timeliness.
Related Issues (20)
- No configuration required per thread (compatibility with timeliness 0.4.0+ HOT 7
- No support for midnight with :time type HOT 1
- NoMethodError: undefined method `deduplicate' with ActiveRecord 6.1 HOT 3
- Doesn't work with Ruby 3.0 / Rails 6.1 (PR attached) HOT 5
- Gem in 5.0.0 version not execute "timeliness" validations in ActiveRecord models HOT 12
- Update `CHANGELOG.rdoc` for v5.0.0 HOT 1
- Check time_zone_aware_types HOT 3
- Plans to release a official 6.x version HOT 3
- `add_error` call with wrong number of arguments HOT 2
- Issue on engine test load HOT 2
- ArgumentError: wrong number of arguments (given 3, expected 1..2) on records.error.add HOT 8
- Locale for Vietnamese HOT 2
- Rails 7 Deprecation Warnings HOT 6
- Rails 7 dependency - activemodel HOT 4
- Please support Rails 7 then cut a new version HOT 1
- Additional maintainer needed HOT 2
- Support for Rails 7 HOT 4
- Is there any way that I can make sure that ranges are exclusive?
- Handle when the passed value is not a Date value. when it's a Numeric
- Comparison to ComparisonValidator HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from validates_timeliness.