characters.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. class Character < ActiveRecord::Base
  2. validates :user_id, presence: true
  3. validates :name, presence: true
  4. validates :species, presence: true
  5. validates :types, presence: true
  6. def self.restricted_find(search, author, flags=[])
  7. # Append Deleted to flags
  8. flags.push('Deleted')
  9. # Find Character
  10. case search.to_i
  11. when 0
  12. where(user_id: author.id).
  13. where.not(active: flags).
  14. find_by!('name ilike ?', search)
  15. else
  16. find(search) if Util::Roles.admin?(author)
  17. end
  18. end
  19. def type_color
  20. Type.find_by(name: types.split("/").first || 'Unknown').color
  21. end
  22. def fetch_inventory
  23. # Fetch item names and amounts in character's inventory
  24. Inventory.where(char_id: id).
  25. joins('join items on items.id = inventories.item_id').
  26. pluck('amount, items.name')
  27. end
  28. def self.from_form(app)
  29. key_mapping = {
  30. "Characters Name" => "name",
  31. "Species" => "species",
  32. "Shiny" => "shiny",
  33. "Type" => "types",
  34. "Age" => "age",
  35. "Weight" => "weight",
  36. "Height" => "height",
  37. "Gender" => "gender",
  38. "Sexual Orientation" => "orientation",
  39. "Relationship Status" => "relationship",
  40. "Attacks" => "attacks",
  41. "Likes" => "likes",
  42. "Dislikes" => "dislikes",
  43. "Personality" => "personality",
  44. "Hometown" => "hometown",
  45. "Warnings" => "warnings",
  46. "Rumors" => "rumors",
  47. "Backstory" => "backstory",
  48. "Recent Events" => "recent_events",
  49. "Other" => "other",
  50. "Rating" => "rating",
  51. "Current Location" => "location",
  52. "DM Notes" => "dm_notes",
  53. "Edit Key (ignore)" => "edit_url",
  54. }
  55. hash = {
  56. "user_id" => nil,
  57. "name" => nil,
  58. "species" => nil,
  59. "shiny" => nil,
  60. "types" => nil,
  61. "age" => nil,
  62. "weight" => nil,
  63. "height" => nil,
  64. "gender" => nil,
  65. "orientation" => nil,
  66. "relationship" => nil,
  67. "attacks" => nil,
  68. "likes" => nil,
  69. "dislikes" => nil,
  70. "personality" => nil,
  71. "backstory" => nil,
  72. "recent_events" => nil,
  73. "other" => nil,
  74. "edit_url" => nil,
  75. "active" => nil,
  76. "dm_notes" => nil,
  77. "location" => nil,
  78. "rumors" => nil,
  79. "hometown" => nil,
  80. "warnings" => nil,
  81. "rating" => nil
  82. }
  83. user_id = UID.match(app.description)
  84. active = case app.title
  85. when /Personal Character/
  86. 'Active'
  87. when /NPC/
  88. 'NPC'
  89. when /Archived Character/
  90. 'Archived'
  91. end
  92. hash["user_id"] = case app.description
  93. when /public/i
  94. 'Public'
  95. when /server/i
  96. 'Server'
  97. else
  98. user_id[1]
  99. end
  100. hash["active"] = active
  101. hash["edit_url"] = app.footer.text
  102. app.fields.each do |field|
  103. next if field.nil?
  104. db_column = key_mapping[field.name]
  105. if db_column == 'shiny'
  106. hash[db_column] = field.value.match(/yes/i) ? true : false
  107. else
  108. hash[db_column] = field.value
  109. end
  110. end
  111. hash = hash.reject { |k,v| k == nil }
  112. hash
  113. end
  114. end