Skip to content

Instantly share code, notes, and snippets.

@aj07
Created June 7, 2016 09:05
Show Gist options
  • Save aj07/54db84129929577fde58f23c2c282441 to your computer and use it in GitHub Desktop.
Save aj07/54db84129929577fde58f23c2c282441 to your computer and use it in GitHub Desktop.
<div class="form-group">
<%= f.label :from_date, "From Date", :class => 'control-label col-md-2' %>
<div class="col-md-10">
<%= f.text_field :from_date, :class => 'form-control date', :placeholder => "Start Date", :value => "#{ f.object.from_date.nil? ? " " : f.object.from_date.now.strftime("%m/%d/%Y")}" %>
</div>
@arvindvyas
Copy link

You can use date picker for selection , if you want to check it on form submission you can use method like this

class YourModel < ActiveRecord::Base
  validate :expiration_date_cannot_be_in_the_past

  def expiration_date_cannot_be_in_the_past
    if expiration_date.present? && expiration_date < Date.today
      errors.add(:expiration_date, "can't be in the past")
    end
  end    
end

@aj07
Copy link
Author

aj07 commented Jun 7, 2016

Still not working, Here,s how I have integrated..

class Promotion
include Mongoid::Document
field :promo_label, type: String
field :promo_code, type: String
field :promo_zone, type: String
field :promo_for, type: String
field :promo_type, type: String

 field :promo_type_value, type: Float, default: 0

 field :promo_days, type: Array, default: []
 field :promo_users, type: Array, default: []
 field :promo_zone_value, type: Array, default: []

 field :store_id, type: BSON::ObjectId
 field :company_id, type: BSON::ObjectId
 field :created_user, type: BSON::ObjectId
 field :updated_user, type: BSON::ObjectId

 field :active, type: Mongoid::Boolean, default: true
 field :no_end_date, type: Mongoid::Boolean, default: false

 field :from_date, :type => Time
 field :to_date, :type => Time
 include Mongoid::Timestamps

 belongs_to :store
 has_many :user_promotions

 PROMOTCODE_SIZE = 6
 PROMOTDAYS = Date::DAYNAMES
 PROMOTTYPES = ["Discount in Amount", "Discount in Percent", "Flat Delivery Fee"]
 ZONES = ["Zipcodes", "Cities", "States"]
 PROMOFOR = ["DeliveryFee", "Grocery"]

 after_initialize :generate_promotion_code
 before_save :set_default

 validates_presence_of :promo_label, :from_date
 validates_uniqueness_of :promo_code

 validate :check_valid_date
 validate :expiration_date_cannot_be_in_the_past

      def expiration_date_cannot_be_in_the_past
        if expiration_date.present? && expiration_date < Date.today
          errors.add(:expiration_date, "can't be in the past")
        end
      end  

 def check_valid_date
    if !self.no_end_date.present? && !self.to_date.present?
        errors.add(:to_date, "End date can't be blank")
    end
 end

 def generate_promotion_code
    if !self.promo_code.present?
        self.promo_code = SecureRandom.hex[0, PROMOTCODE_SIZE].upcase
    end
 end

 def set_default
    promo_days.delete("")
    promo_zone_value.delete("") if !promo_zone_value.nil?
    promo_users.delete("")
    if promo_zone.present? and self.promo_zone_value.empty?
        self.promo_zone = nil
        self.promo_zone_value = []
    end
 end

 def self.flat_delivery_fees
    # start_day = cart.delivery_date.to_date
    where(promo_type: "Flat Delivery Fee", active: true)
 end

def self.update_promotion_default(current_user, cartRec)
promotion = choose_one_default_valid_promotion(current_user, cartRec).first

    if promotion
        cartRec.user_promotions.where(promotion_id: promotion.id).destroy_all
        if promotion.promo_days.include?(cartRec.delivery_date.strftime("%A"))
            up = UserPromotion.new
            up.user_id = current_user.id
            up.promotion_id = promotion.id
            up.cart_id = cartRec.id
            if  up.save
                cartRec.shipping_charges = promotion.promo_type_value
                cartRec.save
            end
        end
    else
        cartRec.user_promotions.destroy_all
    end

end

def self.choose_one_default_valid_promotion(current_user, cartRec)
self.flat_delivery_fees.map { |e|
if e.promo_days.include?(cartRec.delivery_date.strftime("%A"))
if UserPromotion.valid_promotion?(current_user, cartRec, e, cartRec.delivery_date)
e
end
end
}.compact
end

def offer_day(cart)
offer = self
a = {}
b = [](cart.delivery_date..%28cart.delivery_date.to_date + 7.days%29).map { |date|
if offer.promo_days.include?(date.strftime("%A"))
if b.empty?
b << a["#{date.strftime("%A")}"] = date
end
end
}
if b.any?
[b.first.strftime("%d/%m/%Y"), b.first.strftime("%b, %d")]
else
nil
end
end

def self.search(params)
    promotions = order(created_at: :desc)

if params[:promo_code].present?
  promotions = promotions.where(promo_code: params[:promo_code])
end
if params[:promo_for].present?
  promotions = promotions.where(promo_for: params[:promo_for])
end
promotions
end

end

@arvindvyas
Copy link

arvindvyas commented Jun 7, 2016


      def expiration_date_cannot_be_in_the_past
        if expiration_date.present? && expiration_date < Date.today
          errors.add(:expiration_date, "can't be in the past")
        end
      end  

Here expiration_date is the date field , so check with debugger weather you are getting date on it or not if you are getting that then check this condition, it should work but how you have implemented it is looking like you are checking wrong attribute. As I can see you don't have date field with the name of expiration_date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment