Sha256: 584febd59dcae1da07f51fafc5d942e09c46693696ba85bb39c87be19d778f49

Contents?: true

Size: 1.3 KB

Versions: 31

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

require "active_support/concern"

module Decidim
  module Votings
    module Census
      # Definition of the fields required for frontend forms
      module FrontendFields
        extend ActiveSupport::Concern

        included do
          attribute :day, Integer
          attribute :month, Integer
          attribute :year, Integer

          validates :day, :month, :year, presence: true

          validates :day, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: 31 }
          validates :month, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: 12 }
          validates :year, numericality: { only_integer: true, greater_than_or_equal_to: 1900, less_than_or_equal_to: Time.zone.today.year }

          validate :check_birthdate
        end

        def check_birthdate
          return unless birthdate

          errors.add(:birthdate, :invalid) if Date.civil(year, month, day) > Time.zone.today
        rescue Date::Error
          errors.add(:birthdate, :invalid)
        end

        def birthdate
          return unless [year, month, day].all? { |part| part.is_a? Numeric }

          format("%04d%02d%02d", year, month, day) # rubocop:disable Style/FormatStringToken
        end
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
decidim-elections-0.27.9 app/forms/decidim/votings/census/frontend_fields.rb
decidim-elections-0.27.8 app/forms/decidim/votings/census/frontend_fields.rb
decidim-elections-0.27.7 app/forms/decidim/votings/census/frontend_fields.rb
decidim-elections-0.27.6 app/forms/decidim/votings/census/frontend_fields.rb
decidim-elections-0.26.10 app/forms/decidim/votings/census/frontend_fields.rb
decidim-elections-0.26.9 app/forms/decidim/votings/census/frontend_fields.rb
decidim-elections-0.27.5 app/forms/decidim/votings/census/frontend_fields.rb
decidim-elections-0.26.8 app/forms/decidim/votings/census/frontend_fields.rb
decidim-elections-0.27.4 app/forms/decidim/votings/census/frontend_fields.rb
decidim-elections-0.27.3 app/forms/decidim/votings/census/frontend_fields.rb
decidim-elections-0.26.7 app/forms/decidim/votings/census/frontend_fields.rb
decidim-elections-0.26.5 app/forms/decidim/votings/census/frontend_fields.rb
decidim-elections-0.27.2 app/forms/decidim/votings/census/frontend_fields.rb
decidim-elections-0.27.1 app/forms/decidim/votings/census/frontend_fields.rb
decidim-elections-0.26.4 app/forms/decidim/votings/census/frontend_fields.rb
decidim-elections-0.27.0 app/forms/decidim/votings/census/frontend_fields.rb
decidim-elections-0.26.3 app/forms/decidim/votings/census/frontend_fields.rb
decidim-elections-0.27.0.rc2 app/forms/decidim/votings/census/frontend_fields.rb
decidim-elections-0.27.0.rc1 app/forms/decidim/votings/census/frontend_fields.rb
decidim-elections-0.26.2 app/forms/decidim/votings/census/frontend_fields.rb