Skip to content

Instantly share code, notes, and snippets.

@trdev7
Created June 8, 2020 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trdev7/00e4050d04194b6231d83dd795968ded to your computer and use it in GitHub Desktop.
Save trdev7/00e4050d04194b6231d83dd795968ded to your computer and use it in GitHub Desktop.
Product Serializer on Bookthatapp
class ProductSerializer < ActiveModel::Serializer
attributes :id,
:external_id,
:excerpt,
:profile,
:product_id, :product_handle, :product_title, :product_image_url,
:capacity_type, :capacity, :capacity_option1, :capacity_option2, :capacity_option3,
:duration_type, :duration_option_external_id, :duration_option_position, :duration_option, :duration_option_range_variant,
:duration, :max_duration, :min_duration,
:lead_time, :lag_time, :mindate, :maxdate, :range_count_basis,
:all_day,
:location_id,
:calendar_display,
:background_color, :border_color, :text_color,
:scheduled,
:resource_constraints_attributes,
:resource_capacity_json,
:product_locations_attributes,
:option_capacities_attributes,
:option_durations_attributes,
:terms_attributes,
:variants_attributes,
:schedule_attributes,
:shopify_url,
:terms_attributes,
:tag_list,
:check_in_time, :check_out_time,
:visible_in_calendar,
:widget_id,
:headsup_email_addresses,
:input_feeds
def check_in_time
object.check_in_time.strftime("%H:%M") if object.check_in_time.present?
end
def check_out_time
object.check_out_time.strftime("%H:%M")if object.check_out_time.present?
end
def resource_constraints_attributes
object.resource_constraints.map{ |r| { id: r.id,
resource_id: r.resource_id,
_destroy: false }
}
end
def product_locations_attributes
object.product_locations.map{ |r| { id: r.id,
location_id: r.location_id,
_destroy: false }
}
end
def variants_attributes
object.variants.sort{|a, b| a.position <=> b.position}.map{ |r| { id: r.id,
all_day: r.all_day,
duration: r.duration,
duration_units: r.duration_units,
finish_time: r.finish_time.try(:strftime, "%Y-%m-%d %H:%M"),
ignore: r.ignore,
title: r.title,
option1: r.option1,
option2: r.option2,
option3: r.option3,
external_id: r.external_id,
price: r.price,
sku: r.sku,
party_size: r.party_size,
start_time: r.start_time.try(:strftime, "%Y-%m-%d %H:%M"),
requires_shipping: r.requires_shipping }
}
end
def option_capacities_attributes
object.option_capacities.map{ |r| { capacity: r.capacity,
option1: r.option1,
option2: r.option2,
option3: r.option3 }
}
end
def schedule_attributes
ScheduleSerializer.new(object.schedule, root: false)
end
def terms_attributes
CollectionSerializer.new(object.terms, root: false, each_serializer: TermSerializer)
end
def tag_list
object.tags_from(object.shop)
end
def option_durations_attributes
object.option_durations.order(:value).map do |r|
r.attributes.slice('id', 'option_external_id', 'value', 'duration', 'low_range', 'high_range', 'all_day')
end
end
def input_feeds
CollectionSerializer.new(object.input_feeds, root: false, each_serializer: InputFeedSerializer)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment