module Ecoportal module API class V2 class Page class Component class DateField < Page::Component passdate :value passboolean :show_time, :today_button, :past_only passboolean :create_event passthrough :remind_me_in passboolean :renews passthrough :renews_every, :renews_unit passdate :renews_until ISO8601 = "%Y-%m-%dT%H:%M:00Z" # "%Y-%m-%dT%H:%M:00.00Z" def value=(val) doc["value"] = self.class.to_time(val).yield_self do |datetime| datetime = datetime.utc.strftime(ISO8601) if datetime datetime end end def value if val = doc["value"] (Time.parse(val) rescue nil).yield_self do |datetime| datetime.localtime if datetime end end end def create_event=(value) self.past_only = false doc["create_event"] = !!value end # Quick config helper # @param conf [Symbol, Array] # - `:time` to show time # - `:now_button` to display `NOW` button # - `:past` to only allow past dates # - `:event` to enable event # - `:reminder` to set the reminder (requires `:event`) # - `:never` # - `:one_day` # - `:three_days` # - `:one_week` # - `:two_weeks` # - `:one_month` def configure(*conf) conf.each_with_object([]) do |cnf, unused| case cnf when :time self.show_time = true when :now_button self.today_button = true when :past self.past_only = true when :event self.create_event = true when Hash supported = [:reminder] unless (rest = hash_except(cnf.dup, *supported)).empty? unused.push(rest) end if cnf.key?(:reminder) then configure_reminder(cnf[:reminder]) end else unused.push(cnf) end end.yield_self do |unused| super(*unused) end end private def configure_reminder(value) case value when :never, NilClass self.remind_me_in = "never" when :one_day self.remind_me_in = "1.day" when :three_days self.remind_me_in = "3.days" when :one_week self.remind_me_in = "1.week" when :two_weeks self.remind_me_in = "2.weeks" when :one_month self.remind_me_in = "1.month" else # Unsupported end end end end end end end end