Skip to content

Commit

Permalink
+cocoon
Browse files Browse the repository at this point in the history
  • Loading branch information
i5okie committed Jan 17, 2014
1 parent a5da2ae commit 4ce6da7
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 8 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ gem "haml-rails"
gem 'jquery.fileupload-rails'
gem "mediaelement_rails"
gem "selectize-rails"
gem "cocoon"

# Use jquery as the JavaScript library
gem 'jquery-rails'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ GEM
carrierwave-imagesorcery (0.0.5)
carrierwave
image_sorcery (>= 1.1.0)
cocoon (1.2.5)
coderay (1.1.0)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
Expand Down Expand Up @@ -289,6 +290,7 @@ DEPENDENCIES
cancan
carrierwave
carrierwave-imagesorcery
cocoon
coffee-rails (~> 4.0.0)
cucumber-rails
database_cleaner
Expand Down
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//= require foundation
//= require jquery.fileupload
//= require selectize
//= require cocoon
//= require_tree .

$(function(){ $(document).foundation(); });
Expand Down
7 changes: 7 additions & 0 deletions app/assets/javascripts/products.js.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

$("#attachments a.add_fields").data("association-insertion-position", "before").data "association-insertion-node", "this"
$("#attachments").bind "insertion-callback", ->
$(".attached-fields a.add_fields").data("association-insertion-position", "before").data "association-insertion-node", "this"
$(".attached-fields").bind "insertion-callback", ->
$(this).children("#attachedlist").remove()
$(this).children("a.add_fields").hide()
5 changes: 4 additions & 1 deletion app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ def show
# GET /products/new
def new
@product = Product.new
@product.attached.build
@product.documented.build
end

# GET /products/1/edit
def edit
@attachments = Attachment.all
end

# POST /products
Expand Down Expand Up @@ -69,6 +72,6 @@ def set_product

# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:name, :description, :model, :manufacturer, :link, :image, :remote_image_url, :tags, :apms, :tag_list, :apm_list, :category_id, :documents, :attachments)
params.require(:product).permit(:name, :description, :model, :manufacturer, :link, :image, :remote_image_url, :tags, :apms, :tag_list, :apm_list, :category_id, attached_attributes: [:id, :product_id, :attachment_id, :_destroy])
end
end
4 changes: 4 additions & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ class Product < ActiveRecord::Base
acts_as_taggable_on :apms, :applicables

mount_uploader :image, ImageUploader
accepts_nested_attributes_for :attached, :reject_if => :all_blank, :allow_destroy => true
accepts_nested_attributes_for :attachments, :reject_if => :all_blank, :allow_destroy => true
accepts_nested_attributes_for :documented, :reject_if => :all_blank, :allow_destroy => true
accepts_nested_attributes_for :documents, :reject_if => :all_blank, :allow_destroy => true
end
10 changes: 10 additions & 0 deletions app/views/products/_attached_fields.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.nested-fields.attached-fields
.row
.large-12.columns#attachedlist
= f.association :attachment, :collection => Attachment.all(:order => 'name'), :prompt => 'Choose an existing attachment'
.row
.large-12.columns
= link_to_add_association 'or add a new attachment', f, :attachment
.row
.large-12.columns
= link_to_remove_association "remove attachment", f
6 changes: 6 additions & 0 deletions app/views/products/_attachment_fields.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.nested-fields
= f.input :name
= f.input :description
= f.input :filetype
= f.file_field :filelocation

21 changes: 14 additions & 7 deletions app/views/products/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,27 @@
.large-12.columns
%table
%tr
%th Name
%th Description
%th Filetype
%th Filelocation
%th{width: "400px"} Name
%th{width: "20px"} Filetype
%th{width: "200px"} Filelocation
%th
%th
%th

- @product.attachments.each do |attachment|
- @attachments.each do |attachment|
%tr
%td= attachment.name
%td= attachment.description
%td= attachment.filetype
%td= attachment.filelocation
%td= link_to 'Show', attachment
%td= link_to 'Edit', edit_attachment_path(attachment)
%td= link_to 'Destroy', attachment, :method => :delete, :data => { :confirm => 'Are you sure?' }
%td= link_to 'Destroy', attachment, :method => :delete, :data => { :confirm => 'Are you sure?' }
.row
.large-12.columns#attachments
= f.simple_fields_for :attached do |attached|
.row
.large-12.columns
= render 'attached_fields', :f => attached
.row
.large-12.columns
= link_to_add_association 'add attachment', f, :attached

0 comments on commit 4ce6da7

Please sign in to comment.