GithubHelp home page GithubHelp logo

Comments (4)

adzap avatar adzap commented on July 17, 2024

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.

kfifer avatar kfifer commented on July 17, 2024

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.

adzap avatar adzap commented on July 17, 2024

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.

adzap avatar adzap commented on July 17, 2024

Did you ever figure this out?

from validates_timeliness.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.