<commit_before>class ApplicationController < ActionController::Base
  protect_from_forgery
  layout 'application'
end<commit_msg>Dirty monkey patching in application controller to add support for date|time field to form helpers and formtastic.<commit_after>class ApplicationController < ActionController::Base
  protect_from_forgery
  layout 'application'
end

module ActionView
  module Helpers
    module FormHelper
      def date_field(object_name, method, options = {})
        instance_tag = InstanceTag.new(object_name, method, self, options.delete(:object))

        # Let InstanceTag do the object/attribute lookup for us
        value = instance_tag.value(instance_tag.object)

        # value is empty when re-showing field after error, use params  
        options["value"] =  value.to_s(:text_field) if value.is_a?(Date)
        options["value"] ||= params[object_name][method] if params[object_name]

        instance_tag.to_input_field_tag("text", options)
      end

      def time_field(object_name, method, options = {})
        instance_tag = InstanceTag.new(object_name, method, self, options.delete(:object))

        # Let InstanceTag do the object/attribute lookup for us
        value = instance_tag.value(instance_tag.object)

        # value is empty when re-showing field after error, use params
        options["value"] =  value.to_s(:text_field) if (value.is_a?(Time) or value.is_a?(DateTime))
        options["value"] ||= params[object_name][method] if params[object_name]

        instance_tag.to_input_field_tag("text", options)
      end
    end

    class FormBuilder
      def date_field(method, options = {})
        @template.date_field(@object_name, method, objectify_options(options))
      end

      def time_field(method, options = {})
        @template.time_field(@object_name, method, objectify_options(options))
      end
    end
  end
end

# Monkay patch formtastic
class Formtastic::SemanticFormBuilder
  # Outputs a label and standard Rails text field inside the wrapper.
  def date_field_input(method, options)
    basic_input_helper(:date_field, :string, method, options)
  end

  def time_field_input(method, options)
    basic_input_helper(:time_field, :string, method, options)
  end

  # Add :validates_date to requiring validations
  def method_required?(attribute)
    if @object && @object.class.respond_to?(:reflect_on_validations_for)
      attribute_sym = attribute.to_s.sub(/_id$/, '').to_sym

      @object.class.reflect_on_validations_for(attribute_sym).any? do |validation|
        [:validates_presence_of, :validates_date, :validates_time].include?(validation.macro) &&
        validation.name == attribute_sym && !(validation.options.present? && (validation.options[:allow_nil] || validation.options[:allow_blank])) &&
        (validation.options.present? ? options_require_validation?(validation.options) : true)
      end
    else
      @@all_fields_required_by_default
    end
  end
end
{'fraction_non_alphanumeric': 0.0978334461746784, 'fraction_numerical': 0.0, 'mean_word_length': 4.0426621160409555, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5616494', 'n_tokens_mistral': 857, 'n_tokens_neox': 853, 'n_words': 202}