- template list/show is loaded from central server

- buy a template downloads and creates it in DB
- changes in designs index
This commit is contained in:
chris 2012-12-06 23:20:27 +08:00
parent eb53343bdb
commit 6f1748d2b3
12 changed files with 195 additions and 96 deletions

View File

@ -287,7 +287,7 @@ header {
}
#content .main {
float: right;
width: 680px;
width: 676px;
}
#item-info .item-thumb {
width: 210px;

View File

@ -9,6 +9,17 @@ class Admin::PurchasesController < ApplicationController
@purchases = Purchase.all.entries
end
def buy_template
original_file, zip_name = download_template(params[:id])
temp_file = Tempfile.new("temp_file_zip")
temp_file.binmode
temp_file.write original_file
temp_file.rewind
unzip_design(temp_file, zip_name)
temp_file.close
temp_file.unlink
end
def download
@purchase = Purchase.first(:conditions => {:id => params[:id]})
download_purchase(@purchase, request.env['REQUEST_URI'].split('admin')[0].chop)
@ -183,4 +194,66 @@ class Admin::PurchasesController < ApplicationController
end
def download_template(id)
uri = URI.parse("http://#{APP_CONFIG['store_ip']}/download/design/#{id}")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
[response.body, (response['content-disposition'].split('filename=')[1].gsub(/[\\\"]|.zip/, '') rescue '')]
end
def unzip_design(input_file, zip_name)
temp_file = Tempfile.new("temp_file")
temp_file.write(input_file.read.force_encoding('UTF-8'))
temp_file.rewind
Zip::ZipFile.open(temp_file) { |zip_file|
design = Design.new.from_json(zip_file.read("#{zip_name}/info.json"))
Dir.mktmpdir('f_path') { |dir|
themes_entries = []
javascripts_entries = []
images_entries = []
screenshots_entries = []
zip_file.entries.each do |entry|
case (path = entry.to_s)
when /\A(#{zip_name})\/(default\.css)\z/ #for default css
design.build_css_default(:file => get_temp_file(zip_file, dir, entry))
when /\A(#{zip_name})\/(reset\.css)\z/ #for reset css
design.build_css_reset(:file => get_temp_file(zip_file, dir, entry))
when /\A(#{zip_name})\/(layout\.html)\z/ #for layout html
design.build_layout(:file => get_temp_file(zip_file, dir, entry))
when /\A(#{zip_name})\/(thumb((\.jpg)|(\.png)|(\.gif)))\z/ #for thumb
design.build_thumb(:file => get_temp_file(zip_file, dir, entry))
when /\A(#{zip_name})\/(themes)\/.*(\.css)\z/ #for themes css
themes_entries << entry
when /\A(#{zip_name})\/(javascripts)\/.*(\.js)\z/ #for js
javascripts_entries << entry
when /\A(#{zip_name})\/(images)\/.*((\.jpg)|(\.png)|(\.gif))\z/ #for img
images_entries << entry
when /\A(#{zip_name})\/(screenshots)\/.*((\.jpg)|(\.png)|(\.gif))\z/ #for img
screenshots_entries << entry
end
end
['themes', 'javascripts', 'images', 'screenshots'].each do |type|
eval("#{type}_entries").each do |entry|
eval("design.#{type}").build(:file => get_temp_file(zip_file, dir, entry))
end
end
}
design.zip_file = input_file
design.save
}
temp_file.close
temp_file.unlink
end
def get_temp_file(zip_file, dir, entry)
filename = File.basename(entry.to_s)
temp_file = File.new(dir + '/' + filename, 'w+')
temp_file.write (zip_file.read entry ).force_encoding('UTF-8')
temp_file
end
end

View File

@ -3,9 +3,29 @@ class Admin::TemplateStoreController < ApplicationController
layout "new_admin"
def index
@templates = JSON.parse(get_templates)
end
def show
@template = JSON.parse(get_template(params[:id]))
end
protected
def get_template(id)
uri = URI.parse("http://#{APP_CONFIG['store_ip']}/store/design/#{id}")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
response.body
end
def get_templates
uri = URI.parse("http://#{APP_CONFIG['store_ip']}/store/designs")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
response.body
end
end

View File

@ -8,63 +8,27 @@ class Design
field :title, :type => String
field :version, :type => String
mount_uploader :zip_file, AssetUploader
has_one :css_default, as: :css, :autosave => true, :dependent => :destroy
has_one :layout, :autosave => true, :dependent => :destroy
has_one :css_reset, :autosave => true, :dependent => :destroy
has_one :thumb, :autosave => true, :dependent => :destroy
has_many :images, as: :imgs, :autosave => true, :dependent => :destroy
has_many :javascripts, as: :js, :autosave => true, :dependent => :destroy
has_many :pages
has_many :screenshots, :autosave => true, :dependent => :destroy
has_many :themes, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :images, :allow_destroy => true
accepts_nested_attributes_for :javascripts, :allow_destroy => true
accepts_nested_attributes_for :screenshots, :allow_destroy => true
accepts_nested_attributes_for :themes, :allow_destroy => true
validates_presence_of :author, :title
after_save :parse_css_for_images
def new_files=(*attrs)
attrs[0].map do |key,items_ary| #Loop by JSs,Themes,Imgs
self.files=([items_ary, key])
end
end
# def javascripts=(*attrs)
# self.files = (attrs << 'javascripts')
# end
#
# def themes=(*attrs)
# self.files = (attrs << 'themes')
# end
#
# def images=(*attrs)
# self.files = (attrs << 'images')
# end
# Update or create the attribute records
def files=(attrs)
case attrs.last
when 'layout'
files = self.layout.build
else
files = eval(attrs.last)
end
attrs[0].each do |a|
if a[:id].blank? && !a[:file].blank?
files.build(:file => a[:file], :to_save => true)
else
files.each do |file|
if file.id.to_s == a[:id]
file.to_destroy = a[:to_destroy]
end
end
end
end
end
protected
def parse_css_for_images

View File

@ -0,0 +1,3 @@
class Screenshot < Image
belongs_to :design
end

View File

@ -0,0 +1,3 @@
class Thumb < Image
belongs_to :design
end

View File

@ -1,6 +1,6 @@
<tr class="with_action">
<td><%= check_box_tag 'to_delete[]', design.id, false, :class => "checkbox_in_list" %></td>
<td>
<td class="span1"><%= check_box_tag 'to_delete[]', design.id, false, :class => "checkbox_in_list" %></td>
<td class="span2">
<%= design.title %>
<div class="quick-edit">
<ul class="nav nav-pills hide">
@ -9,6 +9,6 @@
</ul>
</div>
</td>
<td><%= design.author %></td>
<td><%= design.intro %></td>
<td class="span7"><%= design.intro %></td>
<td class="span2"><%= design.author %></td>
</tr>

View File

@ -1,8 +1,8 @@
<thead>
<tr>
<th class="span1 strong"></th>
<th class="span7"></th>
<th class="span2"></th>
<th class="span7"></th>
<th class="span2"></th>
</tr>
</thead>

View File

@ -1,4 +1,4 @@
<%= render_sort_bar(true, delete_admin_designs_path(:direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options]),
['title', 'title','span7', :title],
['author', 'author', 'span2', :author],
['intro', 'intro', 'span2', :intro]).html_safe %>
['title', 'title','span2', :title],
['intro', 'intro', 'span7', :intro],
['author', 'author', 'span2', :author]).html_safe %>

View File

@ -76,18 +76,20 @@
</div>
<div id="item-list">
<ul class="clearfix">
<li><a href="template_store/show"><img src="" alt="" class="item-thumb"><span class="item-info"><b class="item-name">Template 1</b><span class="item-price">Free</span></span></a></li>
<li><a href="template_store/show"><img src="" alt="" class="item-thumb"><span class="item-info"><b class="item-name">Template 2</b><span class="item-price">Free</span></span></a></li>
<li><a href="template_store/show"><img src="" alt="" class="item-thumb"><span class="item-info"><b class="item-name">Template 3</b><span class="item-price">Free</span></span></a></li>
<li><a href="template_store/show"><img src="" alt="" class="item-thumb"><span class="item-info"><b class="item-name">Template 4</b><span class="item-price">Free</span></span></a></li>
<li><a href="template_store/show"><img src="" alt="" class="item-thumb"><span class="item-info"><b class="item-name">Template 5</b><span class="item-price">Free</span></span></a></li>
<li><a href="template_store/show"><img src="" alt="" class="item-thumb"><span class="item-info"><b class="item-name">Template 6</b><span class="item-price">Free</span></span></a></li>
<li><a href="template_store/show"><img src="" alt="" class="item-thumb"><span class="item-info"><b class="item-name">Template 7</b><span class="item-price">Free</span></span></a></li>
<li><a href="template_store/show"><img src="" alt="" class="item-thumb"><span class="item-info"><b class="item-name">Template 8</b><span class="item-price">Free</span></span></a></li>
<li><a href="template_store/show"><img src="" alt="" class="item-thumb"><span class="item-info"><b class="item-name">Template 9</b><span class="item-price">Free</span></span></a></li>
<li><a href="template_store/show"><img src="" alt="" class="item-thumb"><span class="item-info"><b class="item-name">Template 10</b><span class="item-price">Free</template_store/showspan></span></a></li>
<li><a href=""><img src="" alt="" class="item-thumb"><span class="item-info"><b class="item-name">Template 11</b><span class="item-price">Free</template_store/showspan></span></a></li>
<li><a href=""><img src="" alt="" class="item-thumb"><span class="item-info"><b class="item-name">Template 12</b><span class="item-price">Free</span></span></a></li>
<% @templates.each do |template| %>
<li>
<%= link_to admin_template_store_template_path(template['id']) do %>
<%= image_tag "http://#{APP_CONFIG['store_ip']}#{template['thumb']}", :class => "item-thumb" %>
<span class="item-info">
<b class="item-name"><%= template['title'] %></b>
<span class="item-price">Free</span>
</span>
<% end %>
</li>
<% end %>
<% (12-@templates.size).times do |i| %>
<li><a href="template_store/show"><img src="" alt="" class="item-thumb"><span class="item-info"><b class="item-name">Template <%= i + @templates.size + 1 %></b><span class="item-price">Free</span></span></a></li>
<% end %>
</ul>
</div>
</div>

View File

@ -13,7 +13,39 @@
<%= javascript_include_tag "rulingorbit" %>
<% end %>
<div class="wrapper">
<% if @template %>
<div class="wrapper">
<div id="toolbar">
<div class="fn"><a class="ui btn" href="/admin/template_store">Back to Store</a></div>
</div>
<div id="content" class="clearfix">
<div class="side">
<div id="item-info">
<%= image_tag "http://#{APP_CONFIG['store_ip']}#{@template['thumb']}", :class => "item-thumb" %>
<h2 class="item-name"><%= @template['title'] %></h2>
<%= link_to 'free download', buy_template_admin_purchase_path(@template['id']), :id => "download" %>
<ul class="item-extra-info">
<li><span class="title">views</span><span class="count">15,241</span></li>
<li><span class="title">color</span><span class="color-tag green"></span></li>
</ul>
</div>
</div>
<div class="main clearfix">
<h3>description</h3>
<div id="desc-holder" class="paragraph"><%= @template['intro'] %></div>
<h3>screen shot</h3>
<div id="screen-shot">
<div class="holder">
<% @template['screenshots'].each do |screenshot| %>
<%= image_tag "http://#{APP_CONFIG['store_ip']}#{screenshot}", :class => "preview item-thumb" %>
<% end %>
</div>
</div>
</div>
</div>
</div>
<% else %>
<div class="wrapper">
<div id="toolbar">
<div class="fn"><a class="ui btn" href="/admin/template_store">Back to Store</a></div>
</div>
@ -44,4 +76,5 @@
</div>
</div>
</div>
</div>
</div>
<% end %>

View File

@ -112,6 +112,7 @@ Orbit::Application.routes.draw do
get 'install_app'
end
member do
get 'buy_template'
get 'download'
end
end
@ -144,7 +145,7 @@ Orbit::Application.routes.draw do
match 'module_store' => 'module_store#index'
match 'module_store/show' => 'module_store#show'
match 'template_store' => 'template_store#index'
match 'template_store/show' => 'template_store#show'
match 'template_store/template/:id' => 'template_store#show', :as => :template_store_template
end
# end admin