Skip to content

Commit

Permalink
Add Draft Functionality to ProductRequest
Browse files Browse the repository at this point in the history
Adds Draft Functionality to the ProductRequest model. This enables ProductOwners to request editing privileges for a Product.

* Add Draftsman gem
* Add drafting functionality to ProductRequest
* Create ProductRequest controller for handling ProductOwner-specific requests
  • Loading branch information
lukad03 committed May 6, 2016
1 parent 8f11ca8 commit 1eb869b
Show file tree
Hide file tree
Showing 18 changed files with 136 additions and 24 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ gem "cancancan", "~> 1.10"
gem "coffee-rails", "~> 4.1.0"
gem "delayed_job_active_record"
gem "devise", "~> 3.5.4"
gem "draftsman", "~> 0.4.0"
gem "email_validator"
gem "flutie"
gem 'friendly_id', '~> 5.1.0'
gem "friendly_id", "~> 5.1.0"
gem "haml"
gem "high_voltage"
gem "hogan_assets"
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ GEM
dotenv-rails (2.1.0)
dotenv (= 2.1.0)
railties (>= 4.0, < 5.1)
draftsman (0.4.0)
activerecord (>= 3.0, < 5.0)
easy_translate (0.5.0)
json
thread
Expand Down Expand Up @@ -346,6 +348,7 @@ DEPENDENCIES
delayed_job_active_record
devise (~> 3.5.4)
dotenv-rails
draftsman (~> 0.4.0)
email_validator
factory_girl_rails
flutie
Expand Down
1 change: 1 addition & 0 deletions app/assets/stylesheets/extends/_modal.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
left: 50%;
margin-left: -15em;
opacity: 0;
overflow: hidden;
padding: 2em;
position: fixed;
top: 4em;
Expand Down
23 changes: 23 additions & 0 deletions app/controllers/product_owners/product_requests_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module ProductOwners
class ProductRequestsController < ProductOwnersController
def create
@product_request = ProductRequest.new(product_request_params)

if @product_request.draft_creation
flash[:success] =
I18n.t("product_owners.product_requests.create.success")
else
flash[:error] =
I18n.t("product_owners.product_requests.create.failure")
end
redirect_to product_path(@product_request.product)
end

private

def product_request_params
params.require(:product_request).
permit(:product_id).merge(user: signed_in_user)
end
end
end
34 changes: 18 additions & 16 deletions app/controllers/product_owners/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
class ProductOwners::RegistrationsController < RegistrationsController
respond_to :html, :json
layout "minimal"
module ProductOwners
class RegistrationsController < RegistrationsController
respond_to :html, :json
layout "minimal"

private
private

def account_update_params
params.require(:product_owner).
permit(
:first_name,
:last_name,
:email,
:password,
:current_password)
end
def account_update_params
params.require(:product_owner).
permit(
:first_name,
:last_name,
:email,
:password,
:current_password)
end

def sign_up_params
params.require(:product_owner).
permit(:first_name, :last_name, :email, :password)
def sign_up_params
params.require(:product_owner).
permit(:first_name, :last_name, :email, :password)
end
end
end
6 changes: 6 additions & 0 deletions app/controllers/product_owners_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ProductOwnersController < ApplicationController
load_and_authorize_resource
rescue_from CanCan::AccessDenied do
redirect_to new_product_owner_registration_path
end
end
2 changes: 2 additions & 0 deletions app/models/product_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class ProductRequest < ActiveRecord::Base
belongs_to :product, counter_cache: true
belongs_to :user

has_drafts

validates :product, :user, presence: true
validates_uniqueness_of :user_id, scope: :product_id
end
4 changes: 2 additions & 2 deletions app/views/products/_product_content.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
%i.fa.fa-external-link
- if no_users_signed_in? || product_owner_signed_in?
.product-edit
= simple_form_for [product, product_request] do |f|
= simple_form_for [:product_owners, product_request] do |f|
= f.input :product_id, value: product.id, as: :hidden
- if no_users_signed_in?
= button_tag(class: "modal-trigger", data: {modal_id: "product-owner-modal"}, id: "edit-product-button", type: "submit") do
= button_tag(class: "modal-trigger", data: {modal_id: "product-owner-signup-modal"}, id: "edit-product-button", type: "submit") do
= t(".edit_product_html")
- else
= button_tag(id: "edit-product-button", type: "submit") do
Expand Down
2 changes: 1 addition & 1 deletion app/views/products/_product_owner_modal.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.modal.product-owner-modal#product-owner-modal
.modal.product-owner-modal#product-owner-signup-modal
.slide-1-heading
%h1
= t(".heading")
Expand Down
15 changes: 15 additions & 0 deletions config/initializers/draftsman.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Override global `draft` class. For example, perhaps you want your own class at `app/models/draft.rb` that adds
# extra attributes, validations, associations, methods, etc. Be sure that this new model class extends
# `Draftsman::Draft`.
# Draftsman.draft_class_name = 'Draftsman::Draft'

# Serializer for `object`, `object_changes`, and `previous_draft` columns. To use the JSON serializer, change to
# `Draftsman::Serializers::Json`. You could implement your own serializer if you really wanted to. See files in
# `lib/draftsman/serializers`.
#
# Note: this option is not needed if you're using the PostgreSQL JSON data type for the `object`,
# `object_changes`, and `previous_draft` columns.
# Draftsman.serializer = Draftsman::Serializers::Json

# Field which records when a draft was created.
# Draftsman.timestamp_field = :created_at
4 changes: 4 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ en:
sign_up: Sign Up

product_owners:
product_requests:
create:
failure: Something went wrong. Please try again.
success: Your request to edit this Product has been created.
registrations:
new:
heading: Sign up to list your product Apps.gov marketplace!
Expand Down
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
resources :products, only: [:new, :create]
end

namespace :product_owners do
resources :product_requests, only: [:new, :create]
end

get "products/search", to: "products#search", as: :products_search

resources :categories, only: [:show]
Expand Down
22 changes: 22 additions & 0 deletions db/migrate/20160506161108_create_drafts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class CreateDrafts < ActiveRecord::Migration
def change
create_table :drafts do |t|
t.string :item_type, :null => false
t.integer :item_id, :null => false
t.string :event, :null => false
t.string :whodunnit# :null => false
t.text :object
t.text :previous_draft
t.timestamps :null => false
end

change_table :drafts do |t|
t.index :item_type
t.index :item_id
t.index :event
t.index :whodunnit
t.index :created_at
t.index :updated_at
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddDraftAttributesToProductRequest < ActiveRecord::Migration
def change
add_column :product_requests, :draft_id, :integer
add_column :product_requests, :published_at, :timestamp
end
end
26 changes: 23 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160429195814) do
ActiveRecord::Schema.define(version: 20160506165423) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -59,6 +59,24 @@

add_index "delayed_jobs", ["priority", "run_at"], name: "delayed_jobs_priority", using: :btree

create_table "drafts", force: :cascade do |t|
t.string "item_type", null: false
t.integer "item_id", null: false
t.string "event", null: false
t.string "whodunnit"
t.text "object"
t.text "previous_draft"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "drafts", ["created_at"], name: "index_drafts_on_created_at", using: :btree
add_index "drafts", ["event"], name: "index_drafts_on_event", using: :btree
add_index "drafts", ["item_id"], name: "index_drafts_on_item_id", using: :btree
add_index "drafts", ["item_type"], name: "index_drafts_on_item_type", using: :btree
add_index "drafts", ["updated_at"], name: "index_drafts_on_updated_at", using: :btree
add_index "drafts", ["whodunnit"], name: "index_drafts_on_whodunnit", using: :btree

create_table "keywords", force: :cascade do |t|
t.string "name", null: false
t.datetime "created_at"
Expand Down Expand Up @@ -87,8 +105,10 @@
end

create_table "product_requests", force: :cascade do |t|
t.integer "product_id", null: false
t.integer "user_id", null: false
t.integer "product_id", null: false
t.integer "user_id", null: false
t.integer "draft_id"
t.datetime "published_at"
end

add_index "product_requests", ["product_id"], name: "index_product_requests_on_product_id", using: :btree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

click_on "Edit"

expect(page).to have_text t("product_requests.create.success")
expect(page).
to have_text t("product_owners.product_requests.create.success")
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/models/product_request_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "rails_helper"

describe ProductRequest do
it { should be_draftable }
it { should belong_to :product }
it { should belong_to :user }
it { should validate_presence_of :product }
Expand Down
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require "shoulda/matchers"
require "cancan/matchers"
require "paperclip/matchers"
require "draftsman/frameworks/rspec"

::Shoulda::Matchers.configure do |config|
config.integrate do |with|
Expand Down

0 comments on commit 1eb869b

Please sign in to comment.