Skip to content

Commit cfe79b1

Browse files
committed
Conflicts: app/helpers/utilities/form_options_ext.rb
2 parents f1a464c + 42dbb5d commit cfe79b1

File tree

3 files changed

+117
-119
lines changed

3 files changed

+117
-119
lines changed

app/helpers/recurring_select_helper.rb

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
require "ice_cube"
2-
require_relative "utilities/form_options_ext"
3-
require_relative "utilities/tag_ext"
42

53
module RecurringSelectHelper
64
module FormHelper
@@ -24,4 +22,121 @@ def select_recurring(method, default_schedules = nil, options = {}, html_options
2422
@template.select_recurring(@object_name, method, default_schedules, options.merge(:object => @object), html_options)
2523
end
2624
end
25+
26+
module FormOptionsHelper
27+
def recurring_options_for_select(currently_selected_rule = nil, default_schedules = nil, options = {})
28+
29+
options_array = []
30+
blank_option_label = options[:blank_label] || I18n.t("recurring_select.not_recurring")
31+
blank_option = [blank_option_label, "null"]
32+
separator = [I18n.t("recurring_select.or"), {:disabled => true}]
33+
34+
if default_schedules.blank?
35+
if currently_selected_rule.present?
36+
options_array << blank_option if options[:allow_blank]
37+
options_array << ice_cube_rule_to_option(currently_selected_rule)
38+
options_array << separator
39+
options_array << [I18n.t("recurring_select.change_schedule"), "custom"]
40+
else
41+
options_array << blank_option
42+
options_array << [I18n.t("recurring_select.set_schedule"), "custom"]
43+
end
44+
else
45+
options_array << blank_option if options[:allow_blank]
46+
47+
options_array += default_schedules.collect{|dc|
48+
ice_cube_rule_to_option(dc)
49+
}
50+
51+
if currently_selected_rule.present? and !current_rule_in_defaults?(currently_selected_rule, default_schedules)
52+
options_array << ice_cube_rule_to_option(currently_selected_rule, true)
53+
custom_label = [I18n.t("recurring_select.new_custom_schedule"), "custom"]
54+
else
55+
custom_label = [I18n.t("recurring_select.custom_schedule"), "custom"]
56+
end
57+
58+
options_array << separator
59+
options_array << custom_label
60+
end
61+
62+
options_for_select(options_array, currently_selected_rule.to_json)
63+
end
64+
65+
private
66+
67+
def ice_cube_rule_to_option(supplied_rule, custom = false)
68+
return supplied_rule unless RecurringSelect.is_valid_rule?(supplied_rule)
69+
70+
rule = RecurringSelect.dirty_hash_to_rule(supplied_rule)
71+
ar = [rule.to_s, rule.to_hash.to_json]
72+
73+
if custom
74+
ar[0] << "*"
75+
ar << {"data-custom" => true}
76+
end
77+
78+
ar
79+
end
80+
81+
def current_rule_in_defaults?(currently_selected_rule, default_schedules)
82+
default_schedules.any?{|option|
83+
option == currently_selected_rule or
84+
(option.is_a?(Array) and option[1] == currently_selected_rule)
85+
}
86+
end
87+
end
88+
89+
module SelectHTMLOptions
90+
private
91+
92+
def recurring_select_html_options(html_options)
93+
html_options = html_options.stringify_keys
94+
html_options["class"] = (html_options["class"].to_s.split + ["recurring_select"]).join(" ")
95+
html_options
96+
end
97+
end
98+
99+
if Rails::VERSION::STRING.to_f >= 4.0
100+
# === Rails 4
101+
class RecurringSelectTag < ActionView::Helpers::Tags::Base
102+
include RecurringSelectHelper::FormOptionsHelper
103+
include SelectHTMLOptions
104+
105+
def initialize(object, method, template_object, default_schedules = nil, options = {}, html_options = {})
106+
@default_schedules = default_schedules
107+
@choices = @choices.to_a if @choices.is_a?(Range)
108+
@method_name = method.to_s
109+
@object_name = object.to_s
110+
@html_options = recurring_select_html_options(html_options)
111+
add_default_name_and_id(@html_options)
112+
113+
super(object, method, template_object, options)
114+
end
115+
116+
def render
117+
option_tags = add_options(recurring_options_for_select(value(object), @default_schedules, @options), @options, value(object))
118+
select_content_tag(option_tags, @options, @html_options)
119+
end
120+
end
121+
122+
else
123+
# === Rails 3
124+
class InstanceTag < ActionView::Helpers::InstanceTag
125+
include RecurringSelectHelper::FormOptionsHelper
126+
include SelectHTMLOptions
127+
128+
def to_recurring_select_tag(default_schedules, options, html_options)
129+
html_options = recurring_select_html_options(html_options)
130+
add_default_name_and_id(html_options)
131+
value = value(object)
132+
options = add_options(
133+
recurring_options_for_select(value, default_schedules, options),
134+
options, value
135+
)
136+
content_tag("select", options, html_options)
137+
end
138+
end
139+
end
140+
141+
27142
end

app/helpers/utilities/form_options_ext.rb

Lines changed: 0 additions & 64 deletions
This file was deleted.

app/helpers/utilities/tag_ext.rb

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)