Permalink
Browse files

Added temporary experimental convert_incoming_local_datetime_to_utc p…

…roperty, which accepts incoming date times in local time and converts them to utc for call where the API expects date times in utc
  • Loading branch information...
1 parent af5cf63 commit 33361526e9a1ec6572238961b0a1fc9bfa188d17 @RSpace RSpace committed Nov 15, 2011
Showing with 19 additions and 6 deletions.
  1. +17 −4 lib/podio/active_podio/base.rb
  2. +2 −2 lib/podio/models/meeting.rb
@@ -10,17 +10,22 @@ class Base
attr_accessor :attributes, :error_code, :error_message, :error_parameters, :error_propagate
alias_method :propagate_error?, :error_propagate
- def initialize(attributes = {})
+ def initialize(attributes = {}, options = {})
self.valid_attributes ||= []
attributes ||= {}
self.attributes = Hash[*self.valid_attributes.collect { |n| [n.to_sym, nil] }.flatten].merge(attributes.symbolize_keys)
+
+ @values_from_api = options[:values_from_api] # Used to determine if date times should be converted from local to utc, or are already utc
+
attributes.each do |key, value|
if self.respond_to?("#{key}=".to_sym)
self.send("#{key}=".to_sym, value)
elsif valid_attributes.include?(key.to_sym)
self.send(:[]=, key.to_sym, value)
end
end
+
+ @values_from_api = false
end
def persisted?
@@ -140,12 +145,12 @@ def has_many(name, options = {})
# Returns a single instance of the model
def member(response)
- new(response)
+ new(response, :values_from_api => true)
end
# Returns a simple collection model instances
def list(response)
- response.map! { |item| new(item) }
+ response.map! { |item| new(item, :values_from_api => true) }
response
end
@@ -155,7 +160,7 @@ def list(response)
# * total_count: The total number of records matching the given conditions
def collection(response)
result = Struct.new(:all, :count, :total_count).new(response['items'], response['filtered'], response['total'])
- result.all.map! { |item| new(item) }
+ result.all.map! { |item| new(item, :values_from_api => true) }
result
end
@@ -237,6 +242,14 @@ def define_datetime_accessor(name, options = {})
end
self.send(:define_method, "#{name}=") do |value|
+
+ # TODO: This should eventually be done on all date times
+ # This option is a temporary fix while API transitions to UTC only
+ if options[:convert_incoming_local_datetime_to_utc] && !@values_from_api
+ value = value.try(:to_datetime) unless value.is_a?(DateTime)
+ value = Time.zone.local_to_utc(value)
+ end
+
self[name.to_sym] = if value.is_a?(DateTime)
value.try(:to_s, :db)
else
@@ -3,8 +3,8 @@ class Podio::Meeting < ActivePodio::Base
property :meeting_id, :integer
property :title, :string
- property :starts_on, :datetime
- property :ends_on, :datetime
+ property :starts_on, :datetime, :convert_incoming_local_datetime_to_utc => true
+ property :ends_on, :datetime, :convert_incoming_local_datetime_to_utc => true
property :participant_ids, :array
property :is_remote, :boolean
property :status, :string

0 comments on commit 3336152

Please sign in to comment.