Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/assets/javascripts/content.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ $(document).ready ->
window.scrollTo(0, 0);
), 1

$('.new-attribute-field-link').click (e) ->
e.preventDefault()
$("#attribute-field-modal").openModal()

$('.share').click ->
$('#share-modal').openModal()
6 changes: 6 additions & 0 deletions app/assets/stylesheets/forms.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
color: #999999;
}

.attribute-field-dropdown {
position: absolute;
top: 0;
right: 0;
}

.input-select .select-wrapper {
margin-top: 14px;
margin-bottom: 20px;
Expand Down
15 changes: 15 additions & 0 deletions app/controllers/attribute_categories_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Controller for the Attribute model
class AttributeCategoriesController < ContentController
private

def content_params
params.require(:attribute_category).permit(content_param_list)
end

def content_param_list
[
:user_id, :entity_type,
:name, :label, :icon, :description
]
end
end
38 changes: 38 additions & 0 deletions app/controllers/attribute_fields_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Controller for the Attribute model
class AttributeFieldsController < ContentController
private

def initialize_object
category = current_user.attribute_categories.where(label: content_params[:attribute_category]).first_or_initialize.tap do |c|
c.entity_type = params[:entity_type]
c.save!
end

@content = AttributeField.new(label: content_params[:label]).tap do |f|
f.attribute_category_id = category.id
f.user_id = current_user.id
f.field_type = 'textearea'
end
end

def content_deletion_redirect_url
:back
end

def content_creation_redirect_url
:back
end

def content_params
params.require(:attribute_field).permit(content_param_list)
end

def content_param_list
[
:universe_id, :user_id,
:attribute_category,
:name, :field_type,
:label, :description
]
end
end
1 change: 1 addition & 0 deletions app/controllers/characters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def content_param_list
:motivations, :flaws, :talents, :hobbies, :personality_type,
:fave_color, :fave_food, :fave_possession, :fave_weapon, :fave_animal,
:notes, :private_notes, :privacy,
custom_attribute_values: [:name, :value],
siblingships_attributes: [:id, :sibling_id, :_destroy],
fatherships_attributes: [:id, :father_id, :_destroy],
motherships_attributes: [:id, :mother_id, :_destroy],
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/concerns/has_ownership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def require_ownership
model = content_type_from_controller(self.class)
redirect_if_not_owned model.find(params[:id]), send(redirect_path)
rescue
redirect_to '/500'
redirect_to '/500' unless Rails.env.development?
end

# Unless this content is shared, ensure only the owner can do this action
Expand Down
14 changes: 11 additions & 3 deletions app/controllers/content_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def create
initialize_object

if @content.save
successful_response(@content, t(:create_success, model_name: humanized_model_name))
successful_response(content_creation_redirect_url, t(:create_success, model_name: humanized_model_name))
else
failed_response('new', :unprocessable_entity)
end
Expand All @@ -73,8 +73,7 @@ def destroy
@content = content_type.find(params[:id])
@content.destroy

url = send("#{@content.class.to_s.downcase.pluralize}_path")
successful_response(url, t(:delete_success, model_name: humanized_model_name))
successful_response(content_deletion_redirect_url, t(:delete_success, model_name: humanized_model_name))
end

private
Expand All @@ -90,6 +89,15 @@ def initialize_object
def content_params
params
end

def content_deletion_redirect_url
send("#{@content.class.name.underscore.pluralize}_path")
end

def content_creation_redirect_url
@content
end

def content_symbol
content_type_from_controller(self.class).to_s.downcase.to_sym
end
Expand Down
1 change: 1 addition & 0 deletions app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def content_param_list
:notes, :private_notes, :privacy,

# Relations
custom_attribute_values: [:name, :value],
original_ownerships_attributes: [:id, :original_owner_id, :_destroy],
current_ownerships_attributes: [:id, :current_owner_id, :_destroy],
past_ownerships_attributes: [:id, :past_owner_id, :_destroy],
Expand Down
1 change: 1 addition & 0 deletions app/controllers/locations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def content_param_list

# Relations
#todo might be able to inject/reflect these from :relates concern implementation
custom_attribute_values: [:name, :value],
location_leaderships_attributes: [:id, :leader_id, :_destroy],
capital_cities_relationships_attributes: [:id, :capital_city_id, :_destroy],
largest_cities_relationships_attributes: [:id, :largest_city_id, :_destroy],
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/universes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def content_param_list
:laws_of_physics, :magic_system, :technologies,
:history,
:privacy,
:notes, :private_notes
:notes, :private_notes,
custom_attribute_values: [:name, :value]
]
end
end
14 changes: 14 additions & 0 deletions app/helpers/attributes_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module AttributesHelper
def attribute_category_tab(content, category)
is_disabled = category.attribute_fields.any? do |field|
if content.respond_to?(field.name.to_sym)
content.send(field.name).present?
else
field.attribute_values.where(entity: content).any?
end
end

link = content_tag(:a, category.label, href: "##{category.name.gsub("'", '')}_panel")
content_tag(:li, link, class: "tab col s3 #{is_disabled}")
end
end
8 changes: 8 additions & 0 deletions app/models/attribute.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Attribute < ActiveRecord::Base
belongs_to :user
belongs_to :attribute_field
belongs_to :entity, polymorphic: true

include HasPrivacy
scope :is_public, -> { eager_load(:universe).where('universes.privacy = ? OR attributes.privacy = ?', 'public', 'public') }
end
33 changes: 33 additions & 0 deletions app/models/attribute_category.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class AttributeCategory < ActiveRecord::Base
validates :name, presence: true

belongs_to :user
has_many :attribute_fields

include HasAttributes
include Serendipitous::Concern

before_validation :ensure_name

def self.color
'amber'
end

def self.icon
'tab'
end

def self.content_name
'attribute_category'
end

def icon
self['icon'] || self.class.icon
end

private

def ensure_name
self.name ||= "#{label}-#{Time.now.to_i}".underscore.gsub(' ', '_')
end
end
46 changes: 46 additions & 0 deletions app/models/attribute_field.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class AttributeField < ActiveRecord::Base
validates :name, presence: true

belongs_to :user
belongs_to :attribute_category
has_many :attribute_values, class_name: 'Attribute'

include HasAttributes
include Serendipitous::Concern

attr_accessor :system

before_validation :ensure_name

scope :is_public, -> { eager_load(:universe).where('universes.privacy = ? OR attribute_fields.privacy = ?', 'public', 'public') }

def self.color
'amber'
end

def self.icon
'text_fields'
end

def self.content_name
'attribute'
end

def humanize
label
end

def private?
privacy != 'public'
end

def system?
!!self.system
end

private

def ensure_name
self.name ||= "#{label}-#{Time.now.to_i}".underscore.gsub(' ', '_')
end
end
38 changes: 5 additions & 33 deletions app/models/character.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Character < ActiveRecord::Base

belongs_to :universe

include HasAttributes
include HasPrivacy
include HasContentGroupers
include Serendipitous::Concern
Expand All @@ -39,44 +40,15 @@ def description
role
end

def self.content_name
'character'
end

def self.color
'red'
end

def self.icon
'group'
end

def self.attribute_categories
{
overview: {
icon: 'info',
attributes: %w(name role gender age archetype aliases universe_id)
},
looks: {
icon: 'face',
attributes: %w(weight height haircolor hairstyle facialhair eyecolor race skintone bodytype identmarks)
},
nature: {
icon: 'fingerprint',
attributes: %w(mannerisms motivations flaws prejudices talents hobbies personality_type)
},
social: {
icon: 'groups',
attributes: %w(best_friends archenemies religion politics occupation fave_color fave_food fave_possession fave_weapon fave_animal)
},
history: {
icon: 'info',
attributes: %w(birthday birthplaces education background)
},
family: {
icon: 'device_hub',
attributes: %w(mothers fathers spouses siblings children)
},
notes: {
icon: 'edit',
attributes: %w(notes private_notes)
}
}
end
end
33 changes: 33 additions & 0 deletions app/models/concerns/has_attributes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'active_support/concern'

module HasAttributes
extend ActiveSupport::Concern

included do
attr_accessor :custom_attribute_values
after_save :update_custom_attributes

def self.attribute_categories(user = nil)
categories = YAML.load_file(Rails.root.join('config', 'attributes', "#{content_name}.yml")).map do |category_name, details|
category = AttributeCategory.new(entity_type: self.content_name, name: category_name.to_s, label: details[:label], icon: details[:icon])
category.attribute_fields << details[:attributes].map { |field| AttributeField.new(field.merge(system: true)) }
category
end

return categories if user.nil?
[categories, user.attribute_categories.where(['attribute_categories.entity_type = ?', content_name]).joins(:attribute_fields)].flatten.uniq
end

def update_custom_attributes
(self.custom_attribute_values || []).each do |attribute|
field = user.attribute_fields.find_by_name(attribute[:name])
next if field.nil?

user.attribute_values.where(entity: self, attribute_field_id: field.id).first_or_initialize.tap do |field|
field.value = attribute[:value]
field.save!
end
end
end
end
end
26 changes: 3 additions & 23 deletions app/models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Item < ActiveRecord::Base
belongs_to :user
belongs_to :universe

include HasAttributes
include HasPrivacy
include HasContentGroupers
include Serendipitous::Concern
Expand All @@ -32,28 +33,7 @@ def self.icon
'beach_access'
end

def self.attribute_categories
{
overview: {
icon: 'info',
attributes: %w(name item_type description universe_id)
},
looks: {
icon: 'redeem',
attributes: %w(materials weight)
},
history: {
icon: 'book',
attributes: %w(original_owners past_owners current_owners makers year_made)
},
abilities: {
icon: 'flash_on',
attributes: %w(magic)
},
notes: {
icon: 'edit',
attributes: %w(notes private_notes)
}
}
def self.content_name
'item'
end
end
Loading