Site customization: images uploads
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
class Admin::SiteCustomization::ImagesController < Admin::SiteCustomization::BaseController
|
||||
load_and_authorize_resource :image, class: "SiteCustomization::Image"
|
||||
|
||||
def index
|
||||
@images = SiteCustomization::Image.all_images
|
||||
end
|
||||
|
||||
def update
|
||||
if params[:site_customization_image].nil?
|
||||
redirect_to admin_site_customization_images_path
|
||||
return
|
||||
end
|
||||
|
||||
if @image.update(image_params)
|
||||
redirect_to admin_site_customization_images_path, notice: t('admin.site_customization.images.update.notice')
|
||||
else
|
||||
flash.now[:error] = t('admin.site_customization.images.update.error')
|
||||
|
||||
@images = SiteCustomization::Image.all_images
|
||||
idx = @images.index {|e| e.name == @image.name }
|
||||
@images[idx] = @image
|
||||
|
||||
render :index
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@image.image = nil
|
||||
if @image.save
|
||||
redirect_to admin_site_customization_images_path, notice: t('admin.site_customization.images.destroy.notice')
|
||||
else
|
||||
redirect_to admin_site_customization_images_path, notice: t('admin.site_customization.images.destroy.error')
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def image_params
|
||||
params.require(:site_customization_image).permit(
|
||||
:image
|
||||
)
|
||||
end
|
||||
end
|
||||
@@ -7,7 +7,7 @@ class Admin::SiteCustomization::PagesController < Admin::SiteCustomization::Base
|
||||
|
||||
def create
|
||||
if @page.save
|
||||
redirect_to admin_site_customization_pages_path, notice: t('admin.site_customization.pages.create.notice', link: @page.slug.html_safe)
|
||||
redirect_to admin_site_customization_pages_path, notice: t('admin.site_customization.pages.create.notice')
|
||||
else
|
||||
flash.now[:error] = t('admin.site_customization.pages.create.error')
|
||||
render :new
|
||||
@@ -16,7 +16,7 @@ class Admin::SiteCustomization::PagesController < Admin::SiteCustomization::Base
|
||||
|
||||
def update
|
||||
if @page.update(page_params)
|
||||
redirect_to admin_site_customization_pages_path, notice: t('admin.site_customization.pages.update.notice', link: @page.slug.html_safe)
|
||||
redirect_to admin_site_customization_pages_path, notice: t('admin.site_customization.pages.update.notice')
|
||||
else
|
||||
flash.now[:error] = t('admin.site_customization.pages.update.error')
|
||||
render :edit
|
||||
|
||||
@@ -47,4 +47,8 @@ module ApplicationHelper
|
||||
"<span class='icon-angle-left'></span>".html_safe + t("shared.back")
|
||||
end
|
||||
end
|
||||
|
||||
def image_path_for(filename)
|
||||
SiteCustomization::Image.image_path_for(filename) || filename
|
||||
end
|
||||
end
|
||||
|
||||
@@ -53,6 +53,7 @@ module Abilities
|
||||
can [:index, :create, :edit, :update, :destroy], Geozone
|
||||
|
||||
can :manage, SiteCustomization::Page
|
||||
can :manage, SiteCustomization::Image
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
48
app/models/site_customization/image.rb
Normal file
48
app/models/site_customization/image.rb
Normal file
@@ -0,0 +1,48 @@
|
||||
class SiteCustomization::Image < ActiveRecord::Base
|
||||
VALID_IMAGES = {
|
||||
"icon_home" => [330, 240],
|
||||
"logo_header" => [80, 80],
|
||||
"social-media-icon" => [200, 200],
|
||||
"apple-touch-icon-200" => [200, 200]
|
||||
}
|
||||
|
||||
has_attached_file :image
|
||||
|
||||
validates :name, presence: true, uniqueness: true, inclusion: { in: VALID_IMAGES.keys }
|
||||
validates_attachment_content_type :image, :content_type => ["image/png"]
|
||||
validate :check_image
|
||||
|
||||
def self.all_images
|
||||
VALID_IMAGES.keys.map do |image_name|
|
||||
find_by(name: image_name) || create!(name: image_name.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
def self.image_path_for(filename)
|
||||
image_name = filename.split(".").first
|
||||
|
||||
if i = find_by(name: image_name)
|
||||
i.image.exists? ? i.image.url : nil
|
||||
end
|
||||
end
|
||||
|
||||
def required_width
|
||||
VALID_IMAGES[name].try(:first)
|
||||
end
|
||||
|
||||
def required_height
|
||||
VALID_IMAGES[name].try(:second)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def check_image
|
||||
return unless image?
|
||||
|
||||
dimensions = Paperclip::Geometry.from_file(image.queued_for_write[:original].path)
|
||||
|
||||
errors.add(:image, :image_width, required_width: required_width) unless dimensions.width == required_width
|
||||
errors.add(:image, :image_height, required_height: required_height) unless dimensions.height == required_height
|
||||
end
|
||||
|
||||
end
|
||||
@@ -128,5 +128,11 @@
|
||||
<span class="icon-settings"></span><%= t("admin.menu.site_customization.pages") %>
|
||||
<% end %>
|
||||
</li>
|
||||
|
||||
<li <%= "class=active" if controller_name == "images" %>>
|
||||
<%= link_to admin_site_customization_images_path do %>
|
||||
<span class="icon-settings"></span><%= t("admin.menu.site_customization.images") %>
|
||||
<% end %>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
|
||||
25
app/views/admin/site_customization/images/index.html.erb
Normal file
25
app/views/admin/site_customization/images/index.html.erb
Normal file
@@ -0,0 +1,25 @@
|
||||
<h2><%= t("admin.site_customization.images.index.title") %></h2>
|
||||
|
||||
<table>
|
||||
<tbody>
|
||||
<% @images.each do |image| %>
|
||||
<tr class="<%= image.name %>">
|
||||
<td class="small-12 medium-4">
|
||||
<strong><%= image.name %></strong> (<%= image.required_width %>x<%= image.required_height %>)
|
||||
</td>
|
||||
<td class="small-12 medium-8">
|
||||
<%= form_for([:admin, image], html: { id: "edit_#{dom_id(image)}"}) do |f| %>
|
||||
<div class="small-12 medium-6 large-6 column">
|
||||
<%= image_tag image.image.url if image.image.exists? %>
|
||||
<%= f.file_field :image, label: false %>
|
||||
</div>
|
||||
<div class="small-12 medium-6 large-6 column">
|
||||
<%= f.submit(t('admin.site_customization.images.index.update'), class: "button hollow") %>
|
||||
<%= link_to t('admin.site_customization.images.index.delete'), admin_site_customization_image_path(image), method: :delete, class: "button hollow alert" if image.image.exists? %>
|
||||
</div>
|
||||
<% end %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
@@ -18,7 +18,7 @@
|
||||
<div id="responsive-menu">
|
||||
<div class="top-bar-title">
|
||||
<%= link_to admin_root_path, class: "hide-for-small-only" do %>
|
||||
<%= image_tag('logo_header.png', class: 'hide-for-small-only float-left', size: '80x80', alt: t("layouts.header.logo")) %>
|
||||
<%= image_tag(image_path_for('logo_header.png'), class: 'hide-for-small-only float-left', size: '80x80', alt: t("layouts.header.logo")) %>
|
||||
<%= setting['org_name'] %>
|
||||
| <%= t("admin.dashboard.index.title") %>
|
||||
<% end %>
|
||||
@@ -34,4 +34,4 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
</header>
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
<div id="responsive-menu">
|
||||
<div class="top-bar-title">
|
||||
<%= link_to root_path, class: "hide-for-small-only", accesskey: "0" do %>
|
||||
<%= image_tag('logo_header.png', class: 'hide-for-small-only float-left', size: '80x80', alt: t("layouts.header.logo")) %>
|
||||
<%= image_tag(image_path_for('logo_header.png'), class: 'hide-for-small-only float-left', size: '80x80', alt: t("layouts.header.logo")) %>
|
||||
<%= setting['org_name'] %>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
<%= javascript_include_tag "application", 'data-turbolinks-track' => true %>
|
||||
<%= csrf_meta_tags %>
|
||||
<%= favicon_link_tag "favicon.ico" %>
|
||||
<%= favicon_link_tag "apple-touch-icon-200.png",
|
||||
<%= favicon_link_tag image_path_for("apple-touch-icon-200.png"),
|
||||
rel: "icon apple-touch-icon",
|
||||
sizes: "200x200",
|
||||
type: "image/png" %>
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
<div class="auth-image small-12 medium-3 column">
|
||||
<h1 class="logo margin">
|
||||
<%= link_to root_path do %>
|
||||
<%= image_tag('logo_header.png', class: 'float-left', alt: t("layouts.header.logo")) %>
|
||||
<%= image_tag(image_path_for('logo_header.png'), class: 'float-left', alt: t("layouts.header.logo")) %>
|
||||
<%= setting['org_name'] %>
|
||||
<% end %>
|
||||
</h1>
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
<div id="responsive-menu">
|
||||
<div class="top-bar-title">
|
||||
<%= link_to management_root_path, class: "hide-for-small-only" do %>
|
||||
<%= image_tag('logo_header.png', class: 'hide-for-small-only float-left', size: '80x80', alt: t("layouts.header.logo")) %>
|
||||
<%= image_tag(image_path_for('logo_header.png'), class: 'hide-for-small-only float-left', size: '80x80', alt: t("layouts.header.logo")) %>
|
||||
<%= setting['org_name'] %>
|
||||
| <%= t("management.dashboard.index.title") %>
|
||||
<% end %>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<meta name="twitter:site" content="@consul_dev" />
|
||||
<meta name="twitter:title" content="<%= social_title %>" />
|
||||
<meta name="twitter:description" content="<%= social_description %>" />
|
||||
<meta name="twitter:image" content="<%= image_url '/social-media-icon.png' %>" />
|
||||
<meta name="twitter:image" content="<%= image_url image_path_for('social-media-icon.png') %>" />
|
||||
<!-- Facebook OG -->
|
||||
<meta id="ogtitle" property="og:title" content="<%= social_title %>"/>
|
||||
<% if setting['url'] %>
|
||||
@@ -14,7 +14,7 @@
|
||||
<% end %>
|
||||
<meta property="og:type" content="article"/>
|
||||
<meta id="ogurl" property="og:url" content="<%= social_url %>"/>
|
||||
<meta id="ogimage" property="og:image" content="<%= image_url '/social-media-icon.png' %>"/>
|
||||
<meta id="ogimage" property="og:image" content="<%= image_url image_path_for('social-media-icon.png') %>"/>
|
||||
<meta property="og:site_name" content="<%= setting['org_name'] %>"/>
|
||||
<meta id="ogdescription" property="og:description" content="<%= social_description %>"/>
|
||||
<meta property="fb:app_id" content="<%= Rails.application.secrets.facebook_key %>"/>
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
</div>
|
||||
|
||||
<div class="hide-for-small-only small-12 medium-5 column text-center">
|
||||
<%= image_tag("icon_home.png", size: "330x240", alt:"") %>
|
||||
<%= image_tag(image_path_for("icon_home.png"), size: "330x240", alt:"") %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user