Skip to content

Commit 2d0d4ca

Browse files
committed
refactoring
1 parent c659a23 commit 2d0d4ca

File tree

1 file changed

+47
-47
lines changed

1 file changed

+47
-47
lines changed

lib/capybara-bootstrap-datepicker.rb

+47-47
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,71 @@
1-
require "capybara-bootstrap-datepicker/version"
1+
require 'capybara-bootstrap-datepicker/version'
22
require 'rspec/core'
33

44
module Capybara
55
module BootstrapDatepicker
6-
def select_date(value, options = {})
7-
raise "Must pass a hash containing 'from' or 'xpath'" unless options.is_a?(Hash) and [:from, :xpath].any? { |k| options.has_key? k }
6+
def select_date(value, datepicker: :boostrap, format: nil, from: nil, xpath: nil)
7+
fail "Must pass a hash containing 'from' or 'xpath'" if from.nil? && xpath.nil?
88

9-
picker = options.delete :datepicker
10-
format = options.delete :format
11-
from = options.delete :from
12-
xpath = options.delete :xpath
9+
value = Date.parse(value) unless value.respond_to? :to_date
1310

14-
date = value.is_a?(Date) || value.is_a?(Time) ? value : Date.parse(value)
11+
date_input = xpath ? find(:xpath, xpath) : find_field(from)
1512

16-
date_input = xpath ? find(:xpath, xpath, options) : find_field(from, options)
17-
18-
case picker
13+
case datepicker
1914
when :bootstrap
20-
date_input.click
21-
datepicker = find(:xpath, '//body').find('.datepicker')
15+
select_bootstrap_date date_input, value
16+
else
17+
select_simple_date date_input, value
18+
end
19+
end
2220

23-
datepicker_years = datepicker.find('.datepicker-years', visible: false)
24-
datepicker_months = datepicker.find('.datepicker-months', visible: false)
25-
datepicker_days = datepicker.find('.datepicker-days', visible: false)
21+
def select_simple_date(date_input, value)
22+
value = value.strftime format if format.present?
2623

27-
datepicker_current_decade = datepicker_years.find('th.datepicker-switch', visible: false)
28-
datepicker_current_year = datepicker_months.find('th.datepicker-switch', visible: false)
29-
datepicker_current_month = datepicker_days.find('th.datepicker-switch', visible: false)
24+
date_input.set "#{value}\e"
25+
first(:xpath, '//body').click
26+
end
3027

31-
datepicker_current_month.click if datepicker_days.visible?
32-
datepicker_current_year.click if datepicker_months.visible?
28+
def select_bootstrap_date(date_input, value)
29+
date_input.click
30+
picker = find(:xpath, '//body').find('.datepicker')
3331

34-
decade_start, decade_end = datepicker_current_decade.text.split('-').map &:to_i
32+
picker_years = picker.find('.datepicker-years', visible: false)
33+
picker_months = picker.find('.datepicker-months', visible: false)
34+
picker_days = picker.find('.datepicker-days', visible: false)
3535

36-
if date.year < decade_start.to_i
37-
gap = decade_start/10 - date.year/10
38-
gap.times { datepicker_years.find('th.prev').click }
39-
elsif date.year > decade_end
40-
gap = date.year/10 - decade_end/10
41-
gap.times { datepicker_years.find('th.next').click }
42-
end
36+
picker_current_decade = picker_years.find('th.datepicker-switch', visible: false)
37+
picker_current_year = picker_months.find('th.datepicker-switch', visible: false)
38+
picker_current_month = picker_days.find('th.datepicker-switch', visible: false)
4339

44-
datepicker_years.find('.year', text: date.year).click
45-
datepicker_months.find('.month', text: date.strftime("%b")).click
46-
day_xpath = <<-eos
47-
.//*[contains(concat(' ', @class, ' '), ' day ')
48-
and not(contains(concat(' ', @class, ' '), ' old '))
49-
and not(contains(concat(' ', @class, ' '), ' new '))
50-
and normalize-space(text())='#{date.day}']
51-
eos
52-
datepicker_days.find(:xpath, day_xpath).trigger :click
40+
picker_current_month.click if picker_days.visible?
41+
picker_current_year.click if picker_months.visible?
5342

54-
expect(Date.parse date_input.value).to eq date
55-
expect(page).to have_no_css '.datepicker'
56-
when :jquery
57-
raise "jQuery UI datepicker support is not implemented."
58-
else
59-
date = date.strftime format unless format.nil?
43+
decade_start, decade_end = picker_current_decade.text.split('-').map(&:to_i)
6044

61-
date_input.set "#{date}\e"
62-
first(:xpath, '//body').click
45+
if value.year < decade_start.to_i
46+
gap = decade_start / 10 - value.year / 10
47+
gap.times { picker_years.find('th.prev').click }
48+
elsif value.year > decade_end
49+
gap = value.year / 10 - decade_end / 10
50+
gap.times { picker_years.find('th.next').click }
6351
end
52+
53+
picker_years.find('.year', text: value.year).click
54+
picker_months.find('.month', text: value.strftime('%b')).click
55+
day_xpath = <<-eos
56+
.//*[contains(concat(' ', @class, ' '), ' day ')
57+
and not(contains(concat(' ', @class, ' '), ' old '))
58+
and not(contains(concat(' ', @class, ' '), ' new '))
59+
and normalize-space(text())='#{value.day}']
60+
eos
61+
picker_days.find(:xpath, day_xpath).trigger :click
62+
63+
fail if Date.parse(date_input.value) != value
64+
fail unless page.has_no_css? '.datepicker'
6465
end
6566
end
6667
end
6768

6869
RSpec.configure do |c|
6970
c.include Capybara::BootstrapDatepicker
7071
end
71-

0 commit comments

Comments
 (0)