This commit is contained in:
San Wai Lwin
2018-03-13 14:46:07 +06:30
parent 9ec8c4d18c
commit 694feb9c06
9 changed files with 151 additions and 5 deletions

View File

@@ -164,6 +164,26 @@ $(document).ready(function() {
}
});
$("#shop_image").fileinput({
previewFileType: "image",
allowedFileExtensions: ["jpg", "gif", "png"],
browseClass: "btn btn-success",
browseLabel: "Pick Image",
browseIcon: "<i class=\"fa fa-image\"></i> ",
removeClass: "btn btn-danger",
removeLabel: "Delete",
removeIcon: "<i class=\"fa fa-trash\"></i> ",
showUpload: false,
// uploadClass: "btn btn-info",
// uploadLabel: "Upload",
// uploadIcon: "<i class=\"fa fa-upload\"></i> ",
previewTemplates: {
image: '<div class="file-preview-frame" id="{previewId}" data-fileindex="{fileindex}">\n' +
' <img src="{data}" class="file-preview-image" title="{caption}" alt="{caption}" style="width: 200px;height: 200px;">\n' +
'</div>\n',
}
});
// first input focus for all form
$('form:first *input[data-behaviour!=datepicker]:input[type!=hidden]:first').focus();

View File

@@ -0,0 +1,11 @@
class Settings::DisplayImagesController < ApplicationController
def destroy
#byebug
@item_photo = DisplayImage.find(params[:shop_id])
item = @item_photo.id
@item_photo.destroy
respond_to do |format|
format.html {redirect_to settings_shops_url+'/1/edit', notice: 'Image was successfully destroyed.'}
end
end
end

View File

@@ -1,6 +1,6 @@
class Settings::ShopsController < ApplicationController
load_and_authorize_resource except: [:create]
before_action :set_shop, only: [:show, :edit, :update]
before_action :set_shop, only: [:show, :edit, :update, :destroy]
# GET /settings/shops
# GET /settings/shops.json
@@ -11,11 +11,13 @@ class Settings::ShopsController < ApplicationController
# GET /settings/shops/1
# GET /settings/shops/1.json
def show
@display_images = @settings_shop.display_images.all
end
# GET /settings/shops/new
def new
@settings_shop = Shop.new
@display_image = @settings_shop.display_images.build
end
# GET /settings/shops/1/edit
@@ -28,6 +30,11 @@ class Settings::ShopsController < ApplicationController
@settings_shop = Shop.new(shop_params)
respond_to do |format|
if @settings_shop.save
if params[:display_images].present?
params[:display_images]['image'].each do |a|
@display_image = @settings_shop.display_images.create!(:shop_id => @shop.id, :image => a)
end
end
format.html { redirect_to settings_shops_url, notice: 'Shop was successfully created.' }
format.json { render :index, status: :created, location: @settings_shop }
else
@@ -42,6 +49,11 @@ class Settings::ShopsController < ApplicationController
def update
respond_to do |format|
if @settings_shop.update(shop_params)
if params[:display_images].present?
params[:display_images]['image'].each do |a|
@display_image = @settings_shop.display_images.create!(:shop_id => @shop.id, :image => a)
end
end
format.html { redirect_to settings_shops_url, notice: 'Shop was successfully updated.' }
format.json { render :index, status: :ok, location: @settings_shop }
else
@@ -71,6 +83,6 @@ class Settings::ShopsController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through.
def shop_params
params.require(:shop).permit(:logo, :name,:address,:city,:township,:state,:country,:phone_no,:reservation_no,:license,:activated_at,:license_data,:base_currency,:cloud_token,:cloud_url,:owner_token,:id_prefix,:is_rounding_adj,:quick_sale_summary,:calc_tax_order,:show_account_info)
params.require(:shop).permit(:logo, :name,:address,:city,:township,:state,:country,:phone_no,:reservation_no,:license,:activated_at,:license_data,:base_currency,:cloud_token,:cloud_url,:owner_token,:id_prefix,:is_rounding_adj,:quick_sale_summary,:calc_tax_order,:show_account_info, display_images_attributes: [:id, :shop_id, :image])
end
end

View File

@@ -0,0 +1,5 @@
class DisplayImage < ApplicationRecord
mount_uploader :image, DisplayImageUploader
belongs_to :shop
end

View File

@@ -3,4 +3,7 @@ class Shop < ApplicationRecord
# Shop Image Uploader
mount_uploader :logo, ShopImageUploader
has_many :display_images
accepts_nested_attributes_for :display_images
end

View File

@@ -0,0 +1,59 @@
class DisplayImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# def root
# Rails.root.join 'public/'
# end
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
#"image/image"
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# def cache_dir
# '/tmp/images'
# end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url(*args)
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
# process scale: [200, 300]
process :resize_to_fit => [200, 150]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
# version :thumb do
# process resize_to_fit: [50, 50]
# end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_whitelist
%w(jpg jpeg gif png)
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end

View File

@@ -2,7 +2,7 @@
<div class="col-xs-12 col-sm-12 col-md-8 col-lg-8">
<div class="card">
<div class="body">
<%= simple_form_for([:settings,@settings_shop]) do |f| %>
<%= simple_form_for([:settings,@settings_shop], :html => { :multipart => true }) do |f| %>
<%= f.error_notification %>
<div class="form-group p-l-15">
<%= f.input :name ,:input_html=>{:class=>"col-md-10"},:required=>true%>
@@ -25,7 +25,7 @@
<%= f.input :show_account_info %>
</div>
<label>Shop Image</label>
<label>Shop Logo</label>
<div class="panel padding-10">
<div class="form-group">
<div class="menu-item-img">
@@ -40,6 +40,31 @@
</div>
</div>
<label>Shop Images</label>
<div class="panel padding-10">
<div class="form-group">
<div class="menu-item-img">
<%= f.fields_for :display_images do |p| %>
<div class="field">
<%= p.label :image %><br>
<%= image_tag p.object.image, :class => "img-thumbnail" %>
<%= p.file_field :image, :multiple => true, name: "display_images[image][]" %>
<%= link_to "Destroy", settings_shop_display_image_path(p.object), method: :delete %>
</div>
<% end %>
</div>
</div>
</div>
<!-- <%= f.fields_for :display_images do |p| %>
<div class="field">
<%= p.label :image %><br>
<%= p.file_field :image, :multiple => true, name: "display_images[image][]" %>
</div>
<% end %> -->
<div class="form-actions p-l-15">
<%= f.submit "Submit",:class => 'btn btn-primary btn-lg waves-effect' %>
</div>

View File

@@ -99,6 +99,14 @@
<td style="width:20%">Shop Image</td>
<td><%= image_tag @settings_shop.logo, :size => '200x200'%></td>
</tr>
<tr>
<td style="width:20%">Shop Images</td>
<td>
<% @display_images.each do |p| %>
<%= image_tag p.image , :size => '200x200'%><br>
<% end %>
</td>
</tr>
<tr>
<td><%= link_to t("views.btn.edit"), edit_settings_shop_path(@settings_shop),:class => 'btn btn-primary btn-sm waves-effect' %>
</td>

View File

@@ -285,7 +285,10 @@ scope "(:locale)", locale: /en|mm/ do
#--------- System Settings ------------#
namespace :settings do
resources :shops
resources :shops do
resources :display_images
end
#employees
resources :employees
#menu