diff --git a/.travis.yml b/.travis.yml index 00cae7c..d3cbb05 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,6 @@ before_script: script: - RAILS_ENV=test bundle exec rake db:migrate - bundle exec rspec spec - - bundle exec rubocop --config .rubocop.yml app/models/ app/controllers/ app/helpers/ spec/ + - bundle exec rubocop --config .rubocop.yml app/models/ app/controllers/ app/helpers/ config/initializers/ spec/ services: - postgresql diff --git a/app/controllers/drawings_controller.rb b/app/controllers/drawings_controller.rb index b949843..d3ff234 100644 --- a/app/controllers/drawings_controller.rb +++ b/app/controllers/drawings_controller.rb @@ -61,10 +61,10 @@ def owned_drawing end def drawing_params - params.require(:drawing).permit(:image, :description, :gender, :age, :mood_rating, :subject_matter, :story, :country) + params.require(:drawing).permit(:image, :description, :gender, :age, :mood_rating, :subject_matter, :story, :country, :status) end def set_drawing - @drawing = Drawing.find(params[:id]) + @drawing ||= Drawing.find(params[:id]) end end diff --git a/app/helpers/drawings_helper.rb b/app/helpers/drawings_helper.rb index 1a2f34f..5fbf519 100644 --- a/app/helpers/drawings_helper.rb +++ b/app/helpers/drawings_helper.rb @@ -11,4 +11,11 @@ def drawings_class index_view = current_page?(root_path) || current_page?(controller: "drawings", action: "index") index_view ? "col-xs-6 col-sm-4" : "drawings-full" end + + def radio_statuses + # Example output: [ ["status1", "status1"], ["status2", "status2"] ] + [].tap do |arr| + Drawing.statuses.keys.each { |s| arr << [s, s] } + end + end end diff --git a/app/models/drawing.rb b/app/models/drawing.rb index a53ae31..16f3e0c 100644 --- a/app/models/drawing.rb +++ b/app/models/drawing.rb @@ -1,9 +1,12 @@ class Drawing < ActiveRecord::Base + enum status: %i(pending complete) + validates :image, presence: true validates :age, presence: true, numericality: { only_integer: true } validates :gender, presence: true validates :subject_matter, presence: true validates :mood_rating, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: 10 } + validates :status, presence: true has_attached_file :image, styles: { medium: "640x" } validates_attachment_content_type :image, content_type: %r{\Aimage\/.*\Z} diff --git a/app/views/drawings/_form.html.haml b/app/views/drawings/_form.html.haml index 136adc1..2b1d819 100644 --- a/app/views/drawings/_form.html.haml +++ b/app/views/drawings/_form.html.haml @@ -29,7 +29,10 @@ = f.input :mood_rating, type: 'number', required: true, label: false, placeholder: 'Mood rating (1 to 10)' .form-group.text-center - = f.input :country, priority: TOP_COUNTRIES, selected: (current_user.country if @drawing.new_record?), label: false, include_blank: "Country picture was drawn in", class: 'form-control' + = f.input :country, priority: TOP_COUNTRIES, selected: (@drawing.new_record? ? current_user.country : @drawing.country), label: false, include_blank: "Country picture was drawn in", class: 'form-control' + .form-group.text-center + %label_tag{for: "status"} Status (leave this as Pending if you need to enter required details later) + = f.input :status, collection: radio_statuses, label_method: lambda {|k| k.last.capitalize }, as: :radio_buttons, required: true, label: false .form-group.text-center = f.button :submit, class: 'btn-success btn-block' diff --git a/config/initializers/constants.rb b/config/initializers/constants.rb index 9ff9993..342061b 100644 --- a/config/initializers/constants.rb +++ b/config/initializers/constants.rb @@ -1 +1 @@ -TOP_COUNTRIES = %w(GR HU RS) +TOP_COUNTRIES = %w(GR HU RS).freeze diff --git a/db/migrate/20160807145411_add_status_to_drawings.rb b/db/migrate/20160807145411_add_status_to_drawings.rb new file mode 100644 index 0000000..7aea3ab --- /dev/null +++ b/db/migrate/20160807145411_add_status_to_drawings.rb @@ -0,0 +1,5 @@ +class AddStatusToDrawings < ActiveRecord::Migration + def change + add_column :drawings, :status, :integer, default: 0, index: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 23a2ff2..afe83e0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,15 +11,15 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160806150135) do +ActiveRecord::Schema.define(version: 20160807145411) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" create_table "drawings", force: :cascade do |t| t.string "description" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.string "image_file_name" t.string "image_content_type" t.integer "image_file_size" @@ -31,6 +31,7 @@ t.string "subject_matter" t.text "story" t.string "country" + t.integer "status", default: 0 end add_index "drawings", ["user_id"], name: "index_drawings_on_user_id", using: :btree diff --git a/spec/factories/drawings.rb b/spec/factories/drawings.rb index 750d1eb..fa3ddfc 100644 --- a/spec/factories/drawings.rb +++ b/spec/factories/drawings.rb @@ -8,6 +8,7 @@ subject_matter "Camp life" story "Context of the drawing, e.g. child's back story, cultural notes" country "GR" + status "complete" user end diff --git a/spec/models/drawing_spec.rb b/spec/models/drawing_spec.rb index aca2429..d20bbff 100644 --- a/spec/models/drawing_spec.rb +++ b/spec/models/drawing_spec.rb @@ -3,7 +3,7 @@ RSpec.describe Drawing, type: :model do describe "validations" do context "presence" do - %i(image age gender subject_matter mood_rating).each do |attr| + %i(image age gender subject_matter mood_rating status).each do |attr| it { is_expected.to validate_presence_of attr } end end @@ -19,5 +19,7 @@ is_expected.to validate_numericality_of(:mood_rating) .is_less_than_or_equal_to(10) end + + it { is_expected.to define_enum_for(:status).with(%i(pending complete)) } end end