Created
February 10, 2017 19:19
-
-
Save RJNY/af9edcc6e3be95c8abfa383067bb5bc4 to your computer and use it in GitHub Desktop.
code-sample-3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LayoutAndRowPosition < ActiveRecord::Base | |
default_scope { order('position') } | |
belongs_to :layout | |
belongs_to :row | |
acts_as_list scope: :layout | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Layout < ActiveRecord::Base | |
belongs_to :page | |
has_many :layout_and_row_positions, -> { order 'layout_and_row_positions.position asc' } | |
has_many :rows, through: :layout_and_row_positions | |
accepts_nested_attributes_for :rows | |
validates :title, presence: true, uniqueness: true | |
def activate | |
if self.update_attribute(:active, true) | |
Layout.where("active = true AND page_id = ? AND id NOT IN (?)", page.id, id).update_all(active: false) | |
end | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Cms::LayoutsController < Cms::BaseController | |
before_filter :set_layout, only: [:duplicate, :destroy, :activate, :update] | |
before_filter :get_campaigns, only: :edit | |
respond_to :html, :xml, :json | |
layout "cms" | |
def new | |
@layout = Layout.new(page_id: params[:page_id]) | |
@page = Page.find(@layout.page_id) | |
end | |
def create | |
@layout = Layout.new | |
@layout.assign_attributes(layout_params) | |
@layout.created_by = current_admin_user.email | |
if @layout.save | |
flash[:notice] = "You're pretty good, snake!" | |
else | |
flash[:error] = "Failed to create." | |
end | |
respond_with @layout, location: edit_cms_page_layout_path(@layout.page, @layout) | |
end | |
def edit | |
@layout = Layout.includes(layout_and_row_positions: [row: :tiles]).find(params[:id]) | |
@page = Page.find(@layout.page_id) | |
@row = Row.new | |
end | |
def update | |
@layout.assign_attributes(layout_params) | |
if @layout.save | |
flash[:notice] = "You're pretty good, snake!" | |
else | |
flash[:error] = "Failed to create." | |
end | |
redirect_to :back | |
end | |
def duplicate | |
last_layout_id = Layout.last.id | |
new_layout = @layout.dup | |
new_layout.title += " #{last_layout_id + 1}" | |
new_layout.active = false | |
if new_layout.save | |
@layout.rows.each { |row| new_layout.rows << row } | |
flash[:notice] = "Successfully duped!" | |
else | |
flash[:error] = "nothing happened..." | |
end | |
redirect_to cms_pages_path | |
end | |
def destroy | |
if @layout.destroy | |
flash[:notice] = "Layout has been deleted." | |
else | |
flash[:error] = "something went wrong, please try again..." | |
end | |
respond_with @layout, location: cms_pages_path | |
end | |
def set_layout | |
@layout = Layout.includes(rows: :tiles).find(params[:id]) | |
end | |
def render_partial | |
@tile = Tile.find(params[:tile_id]) | |
@preset = params[:preset] | |
respond_to do |format| | |
format.js | |
end | |
end | |
def activate | |
@layout.activate ? flash[:notice] = "Activated" : flash[:error] = "Nothing happened" | |
redirect_to :back | |
end | |
def get_campaigns | |
@campaigns = Campaign.includes(:promo).all.map { |p| { name: p.promo_title, id: p.id } }.to_json | |
end | |
def layout_params | |
params.require(:layout).permit(:id, :page_id, :active, :title, :title_tag) | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Page < ActiveRecord::Base | |
has_many :layouts, dependent: :destroy | |
has_one :active_layout, -> { where(active: true) }, class_name: "Layout" | |
after_destroy :unset_products_category | |
mount_uploader :bg_img, ImageUploader | |
validates :title, presence: true | |
validates :route, presence: true | |
validate :route_uniqueness | |
validates_format_of :route, with: /\A[\w-]+\z/ | |
scope :public_routes, -> { where(auth: false) } | |
scope :category, -> { includes(:layouts).where(is_category: true, layouts: { active: true }) } | |
scope :not_category, -> { where(is_category: false) } | |
scope :landings, -> { where(is_category: false) } | |
scope :categories, -> { category.map(&:route) } | |
def self.category_mappings | |
mappings = {} | |
category.map do |c| | |
mapping = { c.title.downcase.to_s => c.route } | |
mappings.merge!(mapping) | |
end | |
mappings | |
end | |
DEFAULT_ROUTES = { | |
landing: :LANDING_PAGE, | |
home: :HOME_PAGE, | |
free_samples: :CATEGORIES_PAGE, | |
"free-samples": :CATEGORIES_PAGE, | |
freesamples: :CATEGORIES_PAGE, | |
offers: :OFFERS_PAGE | |
}.freeze | |
def self.find_by_route(route) | |
Page.not_category.find_by(route: route) || DefaultPage.const_get(DEFAULT_ROUTES[route]) | |
end | |
def unset_products_category | |
if is_category? | |
Product.where(filter_category: route).update_all(filter_category: nil) | |
end | |
end | |
def route_uniqueness | |
page = Page.where(title: title, route: route, is_category: is_category).where.not(id: id).first | |
errors[:name] << "Page already exists" if page.present? | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Cms::PagesController < Cms::BaseController | |
before_filter :get_page, only: [:update, :destroy] | |
layout "cms" | |
def index | |
@all_pages = Page.includes(:layouts).all | |
@pages = @all_pages.where(is_category: false).includes(:layouts).all | |
@categories = @all_pages.where(is_category: true).includes(:layouts).all | |
end | |
def create | |
@page = Page.new(page_params) | |
if @page.save | |
flash[:notice] = "Page created Successfully" | |
else | |
flash[:error] = "Failed to create." | |
end | |
redirect_to :back | |
end | |
def update | |
@page.assign_attributes(page_params) | |
if @page.save | |
flash[:notice] = "Page created Successfully" | |
else | |
flash[:error] = "Failed to create." | |
end | |
redirect_to :back | |
end | |
def destroy | |
if @page.destroy | |
flash[:notice] = "Page has been deleted." | |
else | |
flash[:error] = "something went wrong, please try again..." | |
end | |
redirect_to :back | |
end | |
def get_page | |
@page = Page.find(params[:id]) | |
end | |
def page_params | |
params.require(:page).permit(:id, :title, :route, :bg_img, :bg_color, :bg_css, :auth, :is_category, :remove_bg_img) | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PageLayoutsController < ApplicationController | |
before_filter :get_layout | |
before_filter :stylize | |
def show; end | |
private | |
def get_layout | |
@page = get_page | |
if @page | |
@layout = if params[:layout_id].present? | |
@page.layouts.find(params[:layout_id]) | |
else | |
@page.id.nil? ? @page.layouts.first : @page.try(:active_layout) | |
end | |
@rows = @layout.try(:rows) | |
end | |
redirect_to("/404") and return false if @layout.blank? | |
end | |
def get_page | |
klass = user_signed_in? ? Page : Page.public_routes | |
@offers = OffersPageItem.show.ordered | |
if admin_user_signed_in? && params[:id] && params[:layout_id] | |
klass.find(params[:id]) | |
elsif params[:category].present? | |
@category = params[:category] | |
pids = Product.where(filter_category: @category).pluck(:id) | |
@category_samples = Campaign.with_promo_and_product.where(promos: { offerable_id: pids }) | |
klass.category.find_by(route: @category) | |
else | |
route = if params[:slug].present? | |
params[:slug].to_sym | |
elsif user_signed_in? | |
get_action_item | |
:home | |
else | |
:landing | |
end | |
klass.not_category.find_by_route(route) | |
end | |
rescue => e | |
Rails.logger.error e | |
nil | |
end | |
def get_action_item | |
@action_item = current_user.action_item | |
get_action_item_data(@action_item.try(:title)) | |
end | |
def get_action_item_data(action_item) | |
return if action_item.blank? | |
case action_item | |
when "samples" | |
sd = current_user.get_samples | |
@cart_state = current_user.current_cart.state | |
@box_full = sd[:remaining_cart_size] <= 0 | |
@samples = sd[:regular] + sd[:bundle] | |
@selected_samples = sd[:normal_cart] | |
when "pending_feedback" | |
feedback = current_user.feedbacks.includes(promo_offering: :promo) | |
@current_feedback = feedback.current.nonexempt.ordered.unsubmitted | |
end | |
end | |
def stylize | |
@minisheet = "" | |
@rows.each_with_index do |row, index| | |
rcss = [] | |
rcss.push("background-image: url('#{row.bg_img}');") unless row.bg_img.blank? | |
@minisheet += rcss.blank? ? "" : ".l#{@layout.id}__r#{index + 1} { #{rcss.join(' ')} } " | |
@minisheet += row.row_css.blank? ? "" : ".l#{@layout.id}__r#{index + 1} { #{row.row_css} } " | |
@minisheet += row.container_width.blank? ? "" : ".l#{@layout.id}__r--i#{index + 1} { margin: 0 auto; max-width: #{row.container_width}px; } " | |
row.tiles.each_with_index do |t, ti| | |
tcss = [] | |
tcss.push("background-color: #{t.bg_color};") unless t.bg_color.blank? | |
tcss.push("background-image: url('#{t.bg_img}');") unless t.bg_img.blank? | |
@minisheet += tcss.blank? ? "" : ".l#{@layout.id}__r#{index + 1}__t#{ti + 1} { #{tcss.join(' ')} } " | |
@minisheet += t.bg_css.blank? ? "" : ".l#{@layout.id}__r#{index + 1}__t#{ti + 1} { #{t.bg_css} } " | |
end | |
end | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Row < ActiveRecord::Base | |
has_many :layout_and_row_positions, -> { order 'layout_and_row_positions.position asc' }, dependent: :destroy | |
has_many :layouts, through: :layout_and_row_positions | |
has_many :tiles, -> { order(position: :asc) }, dependent: :destroy | |
accepts_nested_attributes_for :tiles, allow_destroy: true | |
mount_uploader :bg_img, ImageUploader | |
validates :title, presence: true, uniqueness: true | |
def used_in_multiple_layouts? | |
layout_and_row_positions.select(:layout_id).count > 1 | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Tile < ActiveRecord::Base | |
belongs_to :row | |
acts_as_list scope: :row | |
mount_uploader :bg_img, ImageUploader | |
header_fields = [:header_txt].freeze | |
bg_fields = [:bg_color, :bg_img, :bg_css, :remove_bg_img].freeze | |
btn_fields = [:btn_txt, :btn_url].freeze | |
sample_field = [:sample_set].freeze | |
all_fields = [header_fields + bg_fields + btn_fields + sample_field].flatten.freeze | |
PRESETS = [ | |
{ name: "As Seen On", partial: "as_seen_on", sizes: [12], input_fields: header_fields + bg_fields }, | |
{ name: "Banner", partial: "banner", sizes: [12], input_fields: header_fields + bg_fields + btn_fields }, | |
{ name: "Blog", partial: "blog_half", sizes: [4, 6, 8], input_fields: header_fields + bg_fields + btn_fields + [:paragraph] }, | |
{ name: "Carousel", partial: "carousel", sizes: [12], input_fields: header_fields + bg_fields + btn_fields }, | |
{ name: "Categories", partial: "categories", sizes: [8, 12], input_fields: bg_fields }, | |
{ name: "↳Categories Banner", partial: "c_banner", sizes: [12], input_fields: bg_fields + header_fields }, | |
{ name: "↳Categories Description", partial: "c_desc", sizes: [4, 6, 8, 12], input_fields: bg_fields + [:paragraph] }, | |
{ name: "↳Categories Nav", partial: "c_nav", sizes: [8, 12], input_fields: bg_fields + header_fields }, | |
{ name: "Linked Image", partial: "linked_image", sizes: [6, 8, 12], input_fields: bg_fields + btn_fields }, | |
{ name: "Offers Full", partial: "offers_full", sizes: [12], input_fields: header_fields + header_fields }, | |
{ name: "Offers Half", partial: "offers_half", sizes: [6], input_fields: header_fields + header_fields }, | |
{ name: "Offers Third", partial: "offers_third", sizes: [4], input_fields: header_fields + header_fields }, | |
{ name: "Samples", partial: "samples", sizes: [4, 6, 8, 12], input_fields: all_fields }, | |
{ name: "Sign in", partial: "signin", sizes: [12], input_fields: bg_fields }, | |
{ name: "Sign up", partial: "signup", sizes: [12], input_fields: header_fields }, | |
{ name: "Three Steps", partial: "three_step", sizes: [12], input_fields: bg_fields + header_fields }, | |
{ name: "Tile Data Dump", partial: "full_form", sizes: [4, 6, 8, 12], input_fields: all_fields }, | |
{ name: "Video", partial: "video", sizes: [12], input_fields: header_fields + bg_fields + [:vid_url] } | |
].freeze | |
PRESET_SIZE = { third: 4, half: 6, two_thirds: 8, full: 12 } | |
validates :title, presence: true, uniqueness: { scope: :row_id } | |
validates :width, presence: true | |
validates :preset, inclusion: { in: PRESETS.map { |p| p[:partial] } }, allow_nil: true | |
def self.presets_of_size(width) | |
accepted_presets = [] | |
PRESETS.each { |preset| accepted_presets << preset if preset[:sizes].include?(width) } | |
accepted_presets | |
end | |
def self.presets_by_size | |
partials_by_size = { third: [], half: [], two_thirds: [], full: [] } | |
size_to_word = PRESET_SIZE.invert | |
PRESETS.each do |preset| | |
preset[:sizes].each { |size| partials_by_size[size_to_word[size]] << preset[:partial] } | |
end | |
partials_by_size | |
end | |
def cleanup_bad_sample_data | |
new_sample_set_data = [] | |
sample_set.split(",").map do |s| | |
new_sample_set_data << s if Campaign.exists?(s) | |
end | |
update_attributes(sample_set: new_sample_set_data.join(",")) | |
end | |
def samples | |
sample_set.blank? ? [] : Campaign.find(sample_set.split(",")) | |
rescue | |
cleanup_bad_sample_data | |
return [] if sample_set.blank? | |
sample_set.blank? ? [] : Campaign.try(:find, sample_set.split(",")) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment