adds proposal model

This commit is contained in:
Juanjo Bazán
2015-09-11 19:24:56 +02:00
parent e454e5300e
commit 104900d343
3 changed files with 100 additions and 3 deletions

57
app/models/proposal.rb Normal file
View File

@@ -0,0 +1,57 @@
class Proposal < ActiveRecord::Base
apply_simple_captcha
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
has_many :comments, as: :commentable
has_many :flags, as: :flaggable
validates :title, presence: true
validates :description, presence: true
validates :author, presence: true
validates :question, presence: true
validate :validate_title_length
validate :validate_description_length
validates :terms_of_service, acceptance: { allow_nil: false }, on: :create
before_validation :sanitize_description
before_validation :sanitize_tag_list
def self.title_max_length
@@title_max_length ||= self.columns.find { |c| c.name == 'title' }.limit || 80
end
def self.description_max_length
6000
end
protected
def sanitize_description
self.description = WYSIWYGSanitizer.new.sanitize(description)
end
def sanitize_tag_list
self.tag_list = TagSanitizer.new.sanitize_tag_list(self.tag_list)
end
private
def validate_description_length
validator = ActiveModel::Validations::LengthValidator.new(
attributes: :description,
minimum: 10,
maximum: Proposal.description_max_length)
validator.validate(self)
end
def validate_title_length
validator = ActiveModel::Validations::LengthValidator.new(
attributes: :title,
minimum: 4,
maximum: Proposal.title_max_length)
validator.validate(self)
end
end

View File

@@ -0,0 +1,22 @@
class CreateProposal < ActiveRecord::Migration
def change
create_table :proposals do |t|
t.string "title", limit: 80
t.text "description"
t.string "question"
t.string "external_url"
t.integer "author_id"
t.datetime "hidden_at"
t.integer "flags_count", default: 0
t.datetime "ignored_flag_at"
t.integer "cached_votes_up", default: 0
t.integer "comments_count", default: 0
t.datetime "confirmed_hide_at"
t.integer "hot_score", limit: 8, default: 0
t.integer "confidence_score", default: 0
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
end

View File

@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150910152734) do ActiveRecord::Schema.define(version: 20150911171301) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@@ -165,7 +165,7 @@ ActiveRecord::Schema.define(version: 20150910152734) do
create_table "locks", force: :cascade do |t| create_table "locks", force: :cascade do |t|
t.integer "user_id" t.integer "user_id"
t.integer "tries", default: 0 t.integer "tries", default: 0
t.datetime "locked_until", default: '2015-09-10 13:46:11', null: false t.datetime "locked_until", default: '2015-09-11 17:24:30', null: false
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
@@ -187,6 +187,24 @@ ActiveRecord::Schema.define(version: 20150910152734) do
add_index "organizations", ["user_id"], name: "index_organizations_on_user_id", using: :btree add_index "organizations", ["user_id"], name: "index_organizations_on_user_id", using: :btree
create_table "proposals", force: :cascade do |t|
t.string "title", limit: 80
t.text "description"
t.string "question"
t.string "external_url"
t.integer "author_id"
t.datetime "hidden_at"
t.integer "flags_count", default: 0
t.datetime "ignored_flag_at"
t.integer "cached_votes_up", default: 0
t.integer "comments_count", default: 0
t.datetime "confirmed_hide_at"
t.integer "hot_score", limit: 8, default: 0
t.integer "confidence_score", default: 0
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "settings", force: :cascade do |t| create_table "settings", force: :cascade do |t|
t.string "key" t.string "key"
t.string "value" t.string "value"
@@ -246,7 +264,7 @@ ActiveRecord::Schema.define(version: 20150910152734) do
t.integer "official_level", default: 0 t.integer "official_level", default: 0
t.datetime "hidden_at" t.datetime "hidden_at"
t.string "sms_confirmation_code" t.string "sms_confirmation_code"
t.string "username", limit: 60 t.string "username", limit: 60
t.string "document_number" t.string "document_number"
t.string "document_type" t.string "document_type"
t.datetime "residence_verified_at" t.datetime "residence_verified_at"