Skip to content

Commit

Permalink
mostly formatting. but dateutil changes too
Browse files Browse the repository at this point in the history
  • Loading branch information
Neeraj Singh committed Dec 22, 2010
1 parent af792ca commit c180059
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 63 deletions.
17 changes: 8 additions & 9 deletions config/routes.rb
Expand Up @@ -6,13 +6,12 @@

namespace(:admin_data) do
scope :admin_data do

controller "crud" do
match '/klass/(:klass)', :to => :index, :as => :index, :via => :get
match '/klass/(:klass)', :to => :create, :as => :index, :via => :post
match '/klass/:klass/new', :to => :new, :as => :new, :via => :get
match '/klass/:klass/:id/del', :to => :del, :as => :del, :via => :delete
match '/klass/:klass/:id/edit', :to => :edit, :as => :edit, :via => :get
match '/klass/(:klass)', :to => :index, :as => :index, :via => :get
match '/klass/(:klass)', :to => :create, :as => :index, :via => :post
match '/klass/:klass/new', :to => :new, :as => :new, :via => :get
match '/klass/:klass/:id/del', :to => :del, :as => :del, :via => :delete
match '/klass/:klass/:id/edit', :to => :edit, :as => :edit, :via => :get
match '/klass/:klass/:id', :to => :show, :via => :get
match '/klass/:klass/:id', :to => :update, :via => :put
match '/klass/:klass/:id', :to => :destroy, :via => :delete
Expand All @@ -24,11 +23,11 @@
end

match '/table_structure/:klass' => "table_structure#index", :as => :table_structure
match '/quick_search/:klass' => "search#quick_search", :as => :search
match '/advance_search/:klass' => "search#advance_search", :as => :advance_search
match '/quick_search/:klass' => "search#quick_search", :as => :search
match '/advance_search/:klass' => "search#advance_search", :as => :advance_search
match '/public/*file' => "public#serve"
root :to => "home#index"

root :to => "home#index"
end
end

Expand Down
4 changes: 1 addition & 3 deletions lib/admin_data.rb
Expand Up @@ -11,12 +11,10 @@ module AdminData
autoload :Config
autoload :ActiveRecordUtil
autoload :SetupConfig
autoload :DateUtil

include SetupConfig

end

require 'admin_data/railtie'

# move date_validation to inside admin_data
require 'admin_data_date_validation'
58 changes: 58 additions & 0 deletions lib/admin_data/date_util.rb
@@ -0,0 +1,58 @@
require 'date'

module AdminData
class DateUtil

# returns a time object for the given input.
# validation is not done. It is assumed that client
# has done validation using .valid? method.
def self.valid?(input)
!!parse(input)
end

# Usage:
#
# parase('13-feb-2008') # => time object
# parse('13-February-2008') # => time object
# parse('13-February-2008') # => time object
# parse('99-Feb-2008') #=> false
#
def self.parse(input)
return false if input.blank?

input.strip!

# remove all the white space characters
input.gsub!(/\s/,'')

return false if input.length < 9

dd,mm,yyyy = input.split('-')
return false if dd.nil? || mm.nil? || yyyy.nil?

# month must be of atleast three characters
return false if mm.length < 3

mm = mm.downcase[0,3]

months = {'jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4, 'may' => 5, 'jun' => 6,
'jul' => 7, 'aug' => 8, 'sep' => 9, 'oct' => 10, 'nov' => 11, 'dec' => 12 }

return false unless months.keys.include? mm

mm = months[mm].to_i
yyyy = yyyy.to_i
dd = dd.to_i

# validate date values
begin
Date.new(yyyy,mm,dd)
rescue
return false
end

Time.now.change(:year => yyyy, :month => mm, :day => dd, :hour => 0)
end

end
end
4 changes: 2 additions & 2 deletions lib/admin_data/search.rb
Expand Up @@ -139,7 +139,7 @@ def compute_search_fields(value)
def values_after_cast
case operator
when /(is_on|is_on_or_before_date|is_on_or_after_date)/
AdminDataDateValidation.validate(operands)
AdminData::DateUtil.parse(operands)
when /(is_equal_to|greater_than|less_than)/
operands.to_i
else
Expand All @@ -150,7 +150,7 @@ def values_after_cast
def validate
case operator
when /(is_on|is_on_or_before_date|is_on_or_after_date)/
"#{operands} is not a valid date" unless AdminDataDateValidation.validate(operands)
"#{operands} is not a valid date" unless AdminData::DateUtil.valid?(operands)
when /(is_equal_to|greater_than|less_than)/
unless operands.blank?
"#{operands} is not a valid integer" unless operands =~ /^\d+$/
Expand Down
49 changes: 0 additions & 49 deletions lib/admin_data_date_validation.rb

This file was deleted.

0 comments on commit c180059

Please sign in to comment.