Skip to content

Commit

Permalink
next step: create image through the api after drop image on gallery.
Browse files Browse the repository at this point in the history
  • Loading branch information
diegorubin committed Jan 17, 2016
1 parent 5c34704 commit a879206
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 9 deletions.
3 changes: 3 additions & 0 deletions app/controllers/admin/api/v1/images_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Admin::Api::V1::ImagesController < Admin::ApiController
end

4 changes: 2 additions & 2 deletions app/models/gallery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class Gallery
# Fields
field :title, type: String

embeds_many :images
accepts_nested_attributes_for :images, allow_destroy: true
embeds_many :items, class_name: 'GalleryItem'
accepts_nested_attributes_for :items, allow_destroy: true

# Validations
validates :title, presence: true
Expand Down
11 changes: 11 additions & 0 deletions app/models/gallery_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class GalleryItem
include Mongoid::Document

field :title, type: String
field :description, type: String
field :image, type: String

embedded_in :gallery

end

1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace :admin do
namespace :api do
namespace :v1 do
resources :images, only: [:create]
resources :markdown, only: [:create]
resources :posts, only: [:index, :create, :update, :destroy]
post '/spellchecker/check', to: 'spellchecker#check', as: 'spellchecker'
Expand Down
10 changes: 4 additions & 6 deletions spec/controllers/admin/galleries_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@
context 'on save gallery with image list' do

let(:gallery_attributes) {FactoryGirl.attributes_for(:gallery)}
let(:gallery_with_image_attributes) do
let(:gallery_with_image_attributes) do
gallery = FactoryGirl.attributes_for(:gallery)
gallery['images_attributes'] = [FactoryGirl.attributes_for(:image)]
gallery['images_attributes'][0]['file'] = Rack::Test::UploadedFile.new(
image_example_path, 'image/jpeg')
gallery['items_attributes'] = [FactoryGirl.attributes_for(:gallery_item)]
gallery
end

it 'create empty gallery' do
post :create, gallery: gallery_attributes
expect(assigns(:gallery).images.size).to eql 0
expect(assigns(:gallery).items.size).to eql 0
end

it 'create with image' do
post :create, gallery: gallery_with_image_attributes
expect(assigns(:gallery).images.size).to eql 1
expect(assigns(:gallery).items.size).to eql 1
expect(response).to be_redirect
end

Expand Down
2 changes: 1 addition & 1 deletion spec/factories/galleries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
title Faker::Commerce.product_name

factory :gallery_with_image do
images {build_list :image, 1}
items {build_list :gallery_item, 1}
end

end
Expand Down
9 changes: 9 additions & 0 deletions spec/factories/gallery_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FactoryGirl.define do

factory :gallery_item do
title Faker::Commerce.product_name
description Faker::Lorem.paragraph(2)
end

end

0 comments on commit a879206

Please sign in to comment.