### Install Validates Timeliness Generator Source: https://github.com/adzap/validates_timeliness/wiki/Upgrading.rdoc Run the generator to create the configuration initializer and I18n file before upgrading. ```bash $ rails generate validates_timeliness:install ``` -------------------------------- ### Install ValidatesTimeliness Gem Source: https://github.com/adzap/validates_timeliness/blob/master/README.md Add the gem to your Gemfile and run bundle install to include it in your project. ```ruby gem 'validates_timeliness', '~> 8.0.0.beta1' ``` ```bash $ bundle install ``` -------------------------------- ### Enable Plugin Parser Source: https://github.com/adzap/validates_timeliness/blob/master/README.md Enable the plugin's date and time parser by setting `config.use_plugin_parser` to true in the setup block. ```ruby # in the setup block config.use_plugin_parser = true ``` -------------------------------- ### Add Time Format Source: https://github.com/adzap/validates_timeliness/wiki/Plugin-Parser.rdoc Add a new custom time format. This example allows parsing times like '10 o'clock'. ```ruby config.parser.add_formats(:time, "d o'clock") ``` -------------------------------- ### Enable Plugin Parser in Configuration Source: https://github.com/adzap/validates_timeliness/wiki/Upgrading.rdoc To enable the plugin parser, set `config.use_plugin_parser` to `true` within the setup block in the initializer file. ```ruby config.use_plugin_parser = true ``` -------------------------------- ### Customizing Error Messages with I18n Source: https://github.com/adzap/validates_timeliness/blob/master/README.md Provides examples of how to define custom error messages for date, time, and datetime validations using the I18n system. ```yaml 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}" ``` -------------------------------- ### Configure Custom Restriction Shorthand Symbols Source: https://github.com/adzap/validates_timeliness/blob/master/README.md Add custom shorthand symbols for temporal restrictions by updating `config.restriction_shorthand_symbols` in the setup block. ```ruby # in the setup block config.restriction_shorthand_symbols.update(yesterday: lambda { 1.day.ago }) ``` -------------------------------- ### Set Default Timezone Source: https://github.com/adzap/validates_timeliness/blob/master/README.md Configure the default timezone for parsing and type casting by setting `config.default_timezone` in the setup block. Defaults to `:utc` if ActiveRecord is not loaded. ```ruby # in the setup block config.default_timezone = :utc ``` -------------------------------- ### Time Validation with Exclusive Before Range Source: https://github.com/adzap/validates_timeliness/blob/master/README.md Validate a time is on or after the start time and strictly before the end time using a Ruby range. ```ruby validates_time :booked_at, between: '9:00am'...'5:00pm' ``` -------------------------------- ### Basic Date Validation in ActiveRecord Source: https://github.com/adzap/validates_timeliness/blob/master/README.md Demonstrates how to use validates_date and the timeliness validator with specific date restrictions. ```ruby class Person < ActiveRecord::Base validates_date :date_of_birth, on_or_before: lambda { Date.current } # or validates :date_of_birth, timeliness: { on_or_before: lambda { Date.current }, type: :date } end ``` -------------------------------- ### Time Validation with Multiple Constraints Source: https://github.com/adzap/validates_timeliness/blob/master/README.md Combine 'on_or_after' and 'before' constraints with custom messages for specific time validation scenarios. ```ruby validates_time :breakfast_time, on_or_after: '6:00am', on_or_after_message: 'must be after opening time', before: :lunchtime, before_message: 'must be before lunch time' ``` -------------------------------- ### Enable Display of Invalid Values in Select Helpers Source: https://github.com/adzap/validates_timeliness/blob/master/README.md Activate the ActionView extension to redisplay invalid date and time values to the user as feedback, instead of blank fields. ```ruby # in the setup block config.enable_date_time_select_extension! ``` -------------------------------- ### Use European Date Formats Source: https://github.com/adzap/validates_timeliness/wiki/Plugin-Parser.rdoc Switch to European date formats to parse 'dd/mm/yy' correctly. This is useful for non-US applications. ```ruby config.parser.use_euro_formats ``` -------------------------------- ### Use US Date Formats Source: https://github.com/adzap/validates_timeliness/wiki/Plugin-Parser.rdoc Explicitly set the parser to use US date formats ('mm/dd/yy'). ```ruby config.parser.use_us_formats ``` -------------------------------- ### Enable Strict Parsing for Select Helpers Source: https://github.com/adzap/validates_timeliness/blob/master/README.md Activate the extension to strictly parse date/time values from select helpers, treating invalid dates as invalid. ```ruby # in the setup block config.enable_multiparameter_extension! ``` -------------------------------- ### Custom Error Message Options Source: https://github.com/adzap/validates_timeliness/blob/master/README.md Illustrates the use of specific validation options for setting custom error messages for various temporal restrictions. ```ruby :invalid_date_message :invalid_time_message :invalid_datetime_message :is_at_message :before_message :on_or_before_message :after_message :on_or_after_message ``` -------------------------------- ### Validating a Specific Record Instance Source: https://github.com/adzap/validates_timeliness/blob/master/README.md Shows how to perform date validation on a specific record instance using the validates_date method. ```ruby @person.validates_date :date_of_birth, on_or_before: lambda { Date.current } ``` -------------------------------- ### Use Restriction Shorthand for Validations Source: https://github.com/adzap/validates_timeliness/blob/master/README.md Utilize shorthand symbols like `:today` for common temporal restrictions in validations. The symbol is evaluated as a lambda. ```ruby validates_date :birth_date, on_or_before: :today ``` -------------------------------- ### Activating ORM Shims for Validates Timeliness Source: https://github.com/adzap/validates_timeliness/blob/master/README.md Configures the validates_timeliness gem to extend ORM support, specifically for ActiveRecord. ```ruby ValidatesTimeliness.setup do |config| # Extend ORM/ODMs for full support (:active_record). config.extend_orms = [ :active_record ] end ``` -------------------------------- ### Configure Date Formats in Locale File Source: https://github.com/adzap/validates_timeliness/wiki/Modifying-Date-Format-In-Error-Messages Add this block to your application's locale file (e.g., config/locales/en.yml) to change the default date, time, and datetime formats used in error messages. ```yaml en: validates_timeliness: error_value_formats: date: '%m/%d/%Y' time: '%H:%M:%S' datetime: '%m/%d/%Y %H:%M:%S' ``` -------------------------------- ### Add Time Format with Higher Precedence Source: https://github.com/adzap/validates_timeliness/wiki/Plugin-Parser.rdoc Add a new time format that takes precedence over an existing one. This ensures 'ss:nn:hh' is interpreted correctly. ```ruby config.parser.add_formats(:time, 'ss:nn:hh', :before => 'hh:nn:ss') ``` -------------------------------- ### Basic Datetime Validation Source: https://github.com/adzap/validates_timeliness/blob/master/README.md Use validates_datetime to ensure a datetime attribute is valid. ```ruby validates_datetime :occurred_at ``` -------------------------------- ### Date Validation with On and On_or_after Restriction Source: https://github.com/adzap/validates_timeliness/blob/master/README.md Validate a date only on create, ensuring it is on or after today. ```ruby validates_date :booked_at, on: :create, on_or_after: :today ``` -------------------------------- ### Customize Dummy Date for Time Types Source: https://github.com/adzap/validates_timeliness/blob/master/README.md Modify the dummy date used for time-only type columns by setting `config.dummy_date_for_time_type` to an array of [year, month, day]. ```ruby # in the setup block config.dummy_date_for_time_type = [2009, 1, 1] ``` -------------------------------- ### Date Validation with Before Constraint Source: https://github.com/adzap/validates_timeliness/blob/master/README.md Validate that a date is before a specific point in time, like 18 years ago, with a custom error message. ```ruby validates_date :date_of_birth, before: lambda { 18.years.ago }, before_message: "must be at least 18 years old" ``` -------------------------------- ### Time Validation with Between Range Source: https://github.com/adzap/validates_timeliness/blob/master/README.md Use a Ruby range object to specify the inclusive time boundaries for validation. ```ruby validates_time :booked_at, between: '9:00am'..'5:00pm' ``` -------------------------------- ### Customize Ambiguous Year Threshold Source: https://github.com/adzap/validates_timeliness/wiki/Plugin-Parser.rdoc Adjust the threshold for interpreting 2-digit years. Years at or above this value are considered last century. ```ruby config.parser.ambiguous_year_threshold = 20 ``` -------------------------------- ### Datetime Validation with After Method Symbol Source: https://github.com/adzap/validates_timeliness/blob/master/README.md Ensure a datetime is after another attribute by referencing it as a method symbol. ```ruby validates_datetime :finish_time, after: :start_time ``` -------------------------------- ### Time Validation with Between Array Source: https://github.com/adzap/validates_timeliness/blob/master/README.md Validate that a time falls within a specified range using an array of strings. ```ruby validates_time :booked_at, between: ['9:00am', '5:00pm'] ``` -------------------------------- ### Define Attribute Methods Hook for ActiveRecord Source: https://github.com/adzap/validates_timeliness/wiki/ORM-Support.rdoc This hook is used to ensure that the ORM's regular attribute method generation completes before the plugin defines its own methods for validated attributes. It also enables the creation of `_before_type_cast` methods. ```ruby def define_attribute_methods super define_timeliness_methods(true) end ``` -------------------------------- ### Control Temporal Restriction Error Display Source: https://github.com/adzap/validates_timeliness/blob/master/README.md Toggle the display of errors related to invalid temporal restriction option values by setting `config.ignore_restriction_errors`. Defaults to false (errors displayed in Rails test mode). ```ruby # in the setup block config.ignore_restriction_errors = true ``` -------------------------------- ### Timezone Awareness Hook for ActiveRecord Source: https://github.com/adzap/validates_timeliness/wiki/ORM-Support.rdoc This hook determines if a given attribute is timezone-aware, which is crucial for accurate timezone handling by the plugin. It leverages ActiveRecord's specific method for checking timezone conversion attributes. ```ruby def timeliness_attribute_timezone_aware?(attr_name) create_time_zone_conversion_attribute?(attr_name, columns_hash[attr_name]) end ``` -------------------------------- ### Remove Date Format Source: https://github.com/adzap/validates_timeliness/wiki/Plugin-Parser.rdoc Remove a specific date format from being considered valid by the parser. ```ruby config.parser.remove_formats(:date, 'm\d\yy') ``` -------------------------------- ### Attribute Type Determination Hook Source: https://github.com/adzap/validates_timeliness/wiki/ORM-Support.rdoc This method is used to retrieve the data type of an attribute, which is necessary for the plugin's parser. It relies on the ORM's internal column hashing mechanism. ```ruby def timeliness_attribute_type(attr_name) columns_hash[attr_name.to_s].type end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.