Skip to content

Commit

Permalink
Merge branch 'master' into jan-15-bugfix-release
Browse files Browse the repository at this point in the history
  • Loading branch information
drusepth committed Jan 16, 2019
2 parents 9d7b987 + 620313d commit b6b2a0b
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 103 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ gem 'slack-notifier'

# Form enhancements
gem 'redcarpet' #markdown formatting
gem 'acts_as_list' #sortables

# Analytics
gem 'mixpanel-ruby'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ GEM
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
acts_as_list (0.9.17)
activerecord (>= 3.0)
addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0)
arel (9.0.0)
Expand Down Expand Up @@ -539,6 +541,7 @@ PLATFORMS
ruby

DEPENDENCIES
acts_as_list
authority
aws-sdk (~> 1.5)
aws-sdk-s3
Expand Down
40 changes: 37 additions & 3 deletions app/controllers/content_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class ContentController < ApplicationController

before_action :set_attributes_content_type, only: [:attributes]

before_action :set_navbar_color
before_action :set_general_navbar_actions, except: [:deleted, :show, :changelog]
before_action :set_navbar_color, except: [:api_sort]
before_action :set_general_navbar_actions, except: [:deleted, :show, :changelog, :api_sort]
before_action :set_specific_navbar_actions, only: [:show, :changelog]
before_action :set_sidenav_expansion
before_action :set_sidenav_expansion, except: [:api_sort]

def index
@content_type_class = content_type_from_controller(self.class)
Expand Down Expand Up @@ -295,6 +295,40 @@ def deleted
end

def attributes
@attribute_categories = @content_type_class.attribute_categories(current_user, show_hidden: true).order(:position)
end

def api_sort #todo
sort_params = params.permit(:content_id, :intended_position, :sortable_class)
sortable_class = sort_params[:sortable_class].constantize # todo audit
return unless sortable_class

content = sortable_class.find_by(id: sort_params[:content_id].to_i)
return unless content.present?
return unless content.user_id == current_user.id
return unless content.respond_to?(:position)

# Ugh not another one of these backfills
if content.position.nil?
content_to_order_first = if content.is_a?(AttributeCategory)
content_type_class = content.entity_type.titleize.constantize
content_type_class.attribute_categories(current_user, show_hidden: true)
elsif content.is_a?(AttributeField)
content.attribute_category.attribute_fields
end

ActiveRecord::Base.transaction do
content_to_order_first.each.with_index do |content_to_order, index|
content_to_order.update_column(:position, 1 + index)
end
end
end

if content.reload && content.insert_at(1 + sort_params[:intended_position].to_i)
render json: 200
else
render json: 500
end
end

private
Expand Down
2 changes: 2 additions & 0 deletions app/models/attribute_category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class AttributeCategory < ApplicationRecord
include Authority::Abilities
self.authorizer_name = 'AttributeAuthorizer'

acts_as_list scope: [:user_id, :entity_type]

before_validation :ensure_name

def self.color
Expand Down
2 changes: 2 additions & 0 deletions app/models/attribute_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class AttributeField < ApplicationRecord

validates_presence_of :user_id

acts_as_list scope: [:user_id, :attribute_category_id]

include HasAttributes
include Serendipitous::Concern

Expand Down
22 changes: 14 additions & 8 deletions app/models/concerns/has_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,28 @@ def self.attribute_categories(user, show_hidden: false)
category = AttributeCategory.with_deleted.find_or_initialize_by(
entity_type: self.content_name,
name: category_name.to_s,
label: details[:label],
user: user,
created_at: "January 1, 1970".to_datetime # um wat
user: user
)
# Default new categories to the default icon
category.icon = details[:icon] unless category.persisted?
# Default new categories to some sane defaults
unless category.persisted?
category.icon = details[:icon]
category.label = details[:label]
end

category.save! if user && category.new_record?
category.attribute_fields << details[:attributes].map do |field|
af_field = category.attribute_fields.with_deleted.find_or_initialize_by(
label: field[:label],
# label: field[:label],
old_column_source: field[:name],
user: user,
field_type: field[:field_type].presence || "text_area"
)
af_field.save! if user && af_field.new_record?
if af_field.label.nil?
af_field.label = field[:label]
end
if user && af_field.new_record?
af_field.save!
end
af_field
end if details.key?(:attributes)

Expand All @@ -53,7 +59,7 @@ def self.attribute_categories(user, show_hidden: false)
.attribute_categories
.where(entity_type: self.content_name, hidden: acceptable_hidden_values)
.eager_load(attribute_fields: :attribute_values)
.order('attribute_categories.created_at, attribute_categories.id')
.order('attribute_categories.position, attribute_categories.created_at, attribute_categories.id')

# We need to do something like this, but... not this.
#.eager_load(attribute_fields: :attribute_values) # .eager_load(:attribute_fields)
Expand Down
7 changes: 6 additions & 1 deletion app/models/serializers/categories_and_fields_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def initialize(categories)
label: field.label,
type: field.field_type,
hidden: !!field.hidden,
position: field.position,
old_column_source: field.old_column_source,
value: ""
}
Expand All @@ -44,7 +45,11 @@ def initialize(categories)
# a[:label] <=> b[:label]
# end

a_value <=> b_value
if a[:position] && b[:position]
a[:position] <=> b[:position]
else
a_value <=> b_value
end
end
}
end
Expand Down
7 changes: 6 additions & 1 deletion app/models/serializers/content_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def initialize(content)
label: field.label,
type: field.field_type,
hidden: !!field.hidden,
position: field.position,
old_column_source: field.old_column_source,
value: self.attribute_values.detect { |value|
value.entity_type == content.page_type &&
Expand All @@ -84,7 +85,11 @@ def initialize(content)
# a[:label] <=> b[:label]
# end

a_value <=> b_value
if a[:position] && b[:position]
a[:position] <=> b[:position]
else
a_value <=> b_value
end
end
}
}
Expand Down
Loading

0 comments on commit b6b2a0b

Please sign in to comment.