OSDN Git Service

create system picture
authoryasushiito <yas@pen-chan.jp>
Mon, 2 Jan 2012 07:34:06 +0000 (16:34 +0900)
committeryasushiito <yas@pen-chan.jp>
Mon, 2 Jan 2012 07:34:06 +0000 (16:34 +0900)
21 files changed:
app/assets/javascripts/system_pictures.js.coffee [new file with mode: 0644]
app/assets/stylesheets/system_pictures.css.scss [new file with mode: 0644]
app/controllers/system_pictures_controller.rb [new file with mode: 0644]
app/helpers/system_pictures_helper.rb [new file with mode: 0644]
app/models/system_picture.rb [new file with mode: 0644]
app/views/system_pictures/index.html.erb [new file with mode: 0644]
app/views/system_pictures/show.html.erb [new file with mode: 0644]
config/environments/development.rb
config/environments/production.rb
config/routes.rb
db/migrate/20120102071851_create_system_pictures.rb [new file with mode: 0644]
lib/picture_io.rb
spec/controllers/system_pictures_controller_spec.rb [new file with mode: 0644]
spec/helpers/system_pictures_helper_spec.rb [new file with mode: 0644]
spec/models/system_picture_spec.rb [new file with mode: 0644]
spec/requests/system_pictures_spec.rb [new file with mode: 0644]
spec/routing/system_pictures_routing_spec.rb [new file with mode: 0644]
spec/views/system_pictures/edit.html.erb_spec.rb [new file with mode: 0644]
spec/views/system_pictures/index.html.erb_spec.rb [new file with mode: 0644]
spec/views/system_pictures/new.html.erb_spec.rb [new file with mode: 0644]
spec/views/system_pictures/show.html.erb_spec.rb [new file with mode: 0644]

diff --git a/app/assets/javascripts/system_pictures.js.coffee b/app/assets/javascripts/system_pictures.js.coffee
new file mode 100644 (file)
index 0000000..7615679
--- /dev/null
@@ -0,0 +1,3 @@
+# 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://jashkenas.github.com/coffee-script/
diff --git a/app/assets/stylesheets/system_pictures.css.scss b/app/assets/stylesheets/system_pictures.css.scss
new file mode 100644 (file)
index 0000000..0d3199b
--- /dev/null
@@ -0,0 +1,3 @@
+// Place all the styles related to the system_pictures controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
diff --git a/app/controllers/system_pictures_controller.rb b/app/controllers/system_pictures_controller.rb
new file mode 100644 (file)
index 0000000..71708b7
--- /dev/null
@@ -0,0 +1,132 @@
+class SystemPicturesController < ApplicationController
+  before_filter :authenticate_admin!, :except => [:index, :show]
+
+  private
+  
+  def set_image(prm)
+    img = nil
+    if (f = prm[:system_picture][:file]).respond_to?(:read)
+      if f.size > 1000000
+        @original_picture.width = 0
+        @original_picture.height = 0
+        @original_picture.ext = 'none'
+        @original_picture.filesize = 1.megabytes
+      else
+        img = Magick::Image.from_blob(f.read).shift
+        @original_picture.width = img.columns
+        @original_picture.height = img.rows
+        @original_picture.ext = img.format.downcase
+        @original_picture.filesize = f.size
+      end
+    else
+      dat = Base64.decode64(prm[:original_picture][:file].to_s.gsub(' ', '+')) #rubyのバグ?+でデコードされるべきキャラがスペースになる
+      img = Magick::Image.from_blob(dat).shift
+      @original_picture.width = img.columns
+      @original_picture.height = img.rows
+      @original_picture.ext = img.format.downcase
+      @original_picture.filesize = 1000
+    end
+    img
+  end
+  
+  # GET /system_pictures
+  # GET /system_pictures.json
+  def index
+    @system_pictures = SystemPicture.all
+
+    respond_to do |format|
+      format.html # index.html.erb
+      format.json { render json: @system_pictures }
+    end
+  end
+
+  # GET /system_pictures/1
+  # GET /system_pictures/1.json
+  def show
+    @system_picture = SystemPicture.find(params[:id])
+
+    respond_to do |format|
+      format.html # show.html.erb
+      format.json { render json: @system_picture }
+    end
+  end
+
+  # GET /system_pictures/new
+  # GET /system_pictures/new.json
+  def new
+    @system_picture = SystemPicture.new
+
+    respond_to do |format|
+      format.html # new.html.erb
+      format.json { render json: @system_picture }
+    end
+  end
+
+  # GET /system_pictures/1/edit
+  def edit
+    @system_picture = SystemPicture.find(params[:id])
+  end
+
+  # POST /system_pictures
+  # POST /system_pictures.json
+  def create
+    @original_picture = OriginalPicture.new
+    img = set_image params
+
+    respond_to do |format|
+      SystemPicture.transaction do
+        if @original_picture.save
+          if @original_picture.store(img)
+            format.html { redirect_to @original_picture, notice: 'Original picture was successfully created.' }
+            format.json { render json: @original_picture, status: :created, location: @original_picture }
+          else
+            format.html { redirect_to @original_picture, notice: 'Failed! Original picture was NOT created.' }
+            format.json { render json: @original_picture.errors, status: :unprocessable_entity }
+          end
+        else
+          format.html { render action: "new" }
+          format.json { render json: @original_picture.errors, status: :unprocessable_entity }
+        end
+      end
+    end
+    @system_picture = SystemPicture.new(params[:system_picture])
+
+    respond_to do |format|
+      if @system_picture.save
+        format.html { redirect_to @system_picture, notice: 'System picture was successfully created.' }
+        format.json { render json: @system_picture, status: :created, location: @system_picture }
+      else
+        format.html { render action: "new" }
+        format.json { render json: @system_picture.errors, status: :unprocessable_entity }
+      end
+    end
+  end
+
+  # PUT /system_pictures/1
+  # PUT /system_pictures/1.json
+  def update
+    @system_picture = SystemPicture.find(params[:id])
+
+    respond_to do |format|
+      if @system_picture.update_attributes(params[:system_picture])
+        format.html { redirect_to @system_picture, notice: 'System picture was successfully updated.' }
+        format.json { head :ok }
+      else
+        format.html { render action: "edit" }
+        format.json { render json: @system_picture.errors, status: :unprocessable_entity }
+      end
+    end
+  end
+
+  # DELETE /system_pictures/1
+  # DELETE /system_pictures/1.json
+  def destroy
+    @system_picture = SystemPicture.find(params[:id])
+    @system_picture.destroy
+
+    respond_to do |format|
+      format.html { redirect_to system_pictures_url }
+      format.json { head :ok }
+    end
+  end
+end
diff --git a/app/helpers/system_pictures_helper.rb b/app/helpers/system_pictures_helper.rb
new file mode 100644 (file)
index 0000000..3616f5c
--- /dev/null
@@ -0,0 +1,2 @@
+module SystemPicturesHelper
+end
diff --git a/app/models/system_picture.rb b/app/models/system_picture.rb
new file mode 100644 (file)
index 0000000..ba4bff8
--- /dev/null
@@ -0,0 +1,32 @@
+class SystemPicture < ActiveRecord::Base
+  def validate
+    errors.add(:filesize, 'size over(1MB)') if self.filesize > 1000000
+  end
+  
+  def dext
+    self.ext.downcase
+  end
+  
+  def filename
+    "#{self.id}.#{self.dext}"
+  end
+  
+  def mime_type
+    "image/#{self.dext}"
+  end
+  
+  def url
+    '/system_pictures/' + filename
+  end
+  
+  def store(rimg)
+    bindata = rimg.to_blob
+    PictureIO.system_picture_io.put bindata, self.filename
+    true
+  end
+  
+  def restore(subdir = nil)
+    PictureIO.system_picture_io.get self.filename, subdir
+  end
+  
+end
diff --git a/app/views/system_pictures/index.html.erb b/app/views/system_pictures/index.html.erb
new file mode 100644 (file)
index 0000000..0143366
--- /dev/null
@@ -0,0 +1,31 @@
+<h1>Listing original_pictures</h1>
+
+<%= render 'uploader' %>
+<table>
+  <tr>
+    <th>Ext</th>
+    <th>Width</th>
+    <th>Height</th>
+    <th>filesize</th>
+    <th>artist</th>
+    <th></th>
+  </tr>
+
+<% @original_pictures.each do |original_picture| %>
+  <tr>
+    <td><%= original_picture.ext %></td>
+    <td><%= original_picture.width %></td>
+    <td><%= original_picture.height %></td>
+    <td><%= original_picture.filesize %></td>
+    <td><%= original_picture.artist_id %></td>
+    <td><%= link_to 'Show', original_picture %></td>
+    <td>
+      <% if author_signed_in? -%>
+        <% if original_picture.own? current_author -%>
+          <%= link_to 'Destroy', original_picture, confirm: 'Are you sure?', method: :delete %>
+        <% end -%>
+      <% end -%>
+    </td>
+  </tr>
+<% end %>
+</table>
diff --git a/app/views/system_pictures/show.html.erb b/app/views/system_pictures/show.html.erb
new file mode 100644 (file)
index 0000000..a48ef44
--- /dev/null
@@ -0,0 +1,48 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+  <b>Ext:</b>
+  <%= @original_picture.ext %>
+</p>
+
+<p>
+  <b>Width:</b>
+  <%= @original_picture.width %>
+</p>
+
+<p>
+  <b>Height:</b>
+  <%= @original_picture.height %>
+</p>
+
+<p>
+  <b>filesize:</b>
+  <%= @original_picture.filesize %>
+</p>
+
+<p>
+  <b>artist:</b>
+  <%= @original_picture.artist_id %>
+</p>
+
+<img src="<%= @original_picture.url -%>">
+
+<p>
+<a href="<%= @original_picture.url %>">browse picture</a> 
+</p>
+<p>
+<%= link_to '再生成', :action => :refresh, :id => @original_picture.id %>
+</p>
+<% if @original_picture.own? current_author -%>
+<p>
+Replace picture
+</p>
+  <%= form_for(@original_picture, :html => { :multipart => true }) do |f| %>
+    <label for="file">File to Upload</label> <%= f.file_field "file" %>
+    <div class="actions">
+      <%= f.submit 'replace' %>
+    </div>
+  <% end %>
+<% end -%>
+
+<%= link_to 'Back', original_pictures_path %>
index 138112a..1f7eb0e 100644 (file)
@@ -35,4 +35,5 @@ require 'local_picture'
 PictureIO.setup do |config|
   config.original_picture_io = PictureIO::LocalPicture.new '/sites/original/pettanr/'
   config.resource_picture_io = PictureIO::LocalPicture.new  '/sites/resource/pettanr/'
+  config.system_picture_io = PictureIO::LocalPicture.new  '/sites/system/pettanr/'
 end
index 71c356d..25028e6 100644 (file)
@@ -71,4 +71,5 @@ require 's3_picture'
 PictureIO.setup do |config|
   config.original_picture_io = PictureIO::S3Picture.new 'pettanr-original'
   config.resource_picture_io = PictureIO::S3Picture.new 'pettanr-stable'
+  config.system_picture_io = PictureIO::S3Picture.new  'pettanr-system'
 end
index 0e0b2c6..3118e85 100644 (file)
@@ -19,6 +19,7 @@ Pettanr::Application.routes.draw do
   resources :lisences, only: [:index]
   resources :original_lisences
   resources :common_lisences
+  resources :system_pictures, only: [:index, :show]
 
   # The priority is based upon order of creation:
   # first created -> highest priority.
diff --git a/db/migrate/20120102071851_create_system_pictures.rb b/db/migrate/20120102071851_create_system_pictures.rb
new file mode 100644 (file)
index 0000000..f62055d
--- /dev/null
@@ -0,0 +1,12 @@
+class CreateSystemPictures < ActiveRecord::Migration
+  def change
+    create_table :system_pictures do |t|
+      t.string :ext, :null => false
+      t.integer :width, :null => false
+      t.integer :height, :null => false
+      t.integer :filesize, :null => false
+
+      t.timestamps
+    end
+  end
+end
index 5ca2662..df49d0e 100644 (file)
@@ -1,6 +1,7 @@
 class PictureIO
   @@resource_picture_io #= LocalPicture.new
   @@original_picture_io #= LocalPicture.new Rails.root + 'original'
+  @@system_picture_io #= LocalPicture.new Rails.root + 'system'
   
   def self.original_picture_io
     @@original_picture_io
@@ -18,6 +19,14 @@ class PictureIO
     @@resource_picture_io = pclass
   end
   
+  def self.system_picture_io
+    @@system_picture_io
+  end
+  
+  def self.system_picture_io=(pclass)
+    @@system_picture_io = pclass
+  end
+  
   def self.setup
     yield self
   end
diff --git a/spec/controllers/system_pictures_controller_spec.rb b/spec/controllers/system_pictures_controller_spec.rb
new file mode 100644 (file)
index 0000000..383f24d
--- /dev/null
@@ -0,0 +1,157 @@
+require 'spec_helper'
+
+# This spec was generated by rspec-rails when you ran the scaffold generator.
+# It demonstrates how one might use RSpec to specify the controller code that
+# was generated by Rails when you ran the scaffold generator.
+#
+# It assumes that the implementation code is generated by the rails scaffold
+# generator.  If you are using any extension libraries to generate different
+# controller code, this generated spec may or may not pass.
+#
+# It only uses APIs available in rails and/or rspec-rails.  There are a number
+# of tools you can use to make these specs even more expressive, but we're
+# sticking to rails and rspec-rails APIs to keep things simple and stable.
+#
+# Compared to earlier versions of this generator, there is very limited use of
+# stubs and message expectations in this spec.  Stubs are only used when there
+# is no simpler way to get a handle on the object needed for the example.
+# Message expectations are only used when there is no simpler way to specify
+# that an instance is receiving a specific message.
+
+describe SystemPicturesController do
+
+  # This should return the minimal set of attributes required to create a valid
+  # SystemPicture. As you add validations to SystemPicture, be sure to
+  # update the return value of this method accordingly.
+  def valid_attributes
+    {}
+  end
+
+  describe "GET index" do
+    it "assigns all system_pictures as @system_pictures" do
+      system_picture = SystemPicture.create! valid_attributes
+      get :index
+      assigns(:system_pictures).should eq([system_picture])
+    end
+  end
+
+  describe "GET show" do
+    it "assigns the requested system_picture as @system_picture" do
+      system_picture = SystemPicture.create! valid_attributes
+      get :show, :id => system_picture.id
+      assigns(:system_picture).should eq(system_picture)
+    end
+  end
+
+  describe "GET new" do
+    it "assigns a new system_picture as @system_picture" do
+      get :new
+      assigns(:system_picture).should be_a_new(SystemPicture)
+    end
+  end
+
+  describe "GET edit" do
+    it "assigns the requested system_picture as @system_picture" do
+      system_picture = SystemPicture.create! valid_attributes
+      get :edit, :id => system_picture.id
+      assigns(:system_picture).should eq(system_picture)
+    end
+  end
+
+  describe "POST create" do
+    describe "with valid params" do
+      it "creates a new SystemPicture" do
+        expect {
+          post :create, :system_picture => valid_attributes
+        }.to change(SystemPicture, :count).by(1)
+      end
+
+      it "assigns a newly created system_picture as @system_picture" do
+        post :create, :system_picture => valid_attributes
+        assigns(:system_picture).should be_a(SystemPicture)
+        assigns(:system_picture).should be_persisted
+      end
+
+      it "redirects to the created system_picture" do
+        post :create, :system_picture => valid_attributes
+        response.should redirect_to(SystemPicture.last)
+      end
+    end
+
+    describe "with invalid params" do
+      it "assigns a newly created but unsaved system_picture as @system_picture" do
+        # Trigger the behavior that occurs when invalid params are submitted
+        SystemPicture.any_instance.stub(:save).and_return(false)
+        post :create, :system_picture => {}
+        assigns(:system_picture).should be_a_new(SystemPicture)
+      end
+
+      it "re-renders the 'new' template" do
+        # Trigger the behavior that occurs when invalid params are submitted
+        SystemPicture.any_instance.stub(:save).and_return(false)
+        post :create, :system_picture => {}
+        response.should render_template("new")
+      end
+    end
+  end
+
+  describe "PUT update" do
+    describe "with valid params" do
+      it "updates the requested system_picture" do
+        system_picture = SystemPicture.create! valid_attributes
+        # Assuming there are no other system_pictures in the database, this
+        # specifies that the SystemPicture created on the previous line
+        # receives the :update_attributes message with whatever params are
+        # submitted in the request.
+        SystemPicture.any_instance.should_receive(:update_attributes).with({'these' => 'params'})
+        put :update, :id => system_picture.id, :system_picture => {'these' => 'params'}
+      end
+
+      it "assigns the requested system_picture as @system_picture" do
+        system_picture = SystemPicture.create! valid_attributes
+        put :update, :id => system_picture.id, :system_picture => valid_attributes
+        assigns(:system_picture).should eq(system_picture)
+      end
+
+      it "redirects to the system_picture" do
+        system_picture = SystemPicture.create! valid_attributes
+        put :update, :id => system_picture.id, :system_picture => valid_attributes
+        response.should redirect_to(system_picture)
+      end
+    end
+
+    describe "with invalid params" do
+      it "assigns the system_picture as @system_picture" do
+        system_picture = SystemPicture.create! valid_attributes
+        # Trigger the behavior that occurs when invalid params are submitted
+        SystemPicture.any_instance.stub(:save).and_return(false)
+        put :update, :id => system_picture.id, :system_picture => {}
+        assigns(:system_picture).should eq(system_picture)
+      end
+
+      it "re-renders the 'edit' template" do
+        system_picture = SystemPicture.create! valid_attributes
+        # Trigger the behavior that occurs when invalid params are submitted
+        SystemPicture.any_instance.stub(:save).and_return(false)
+        put :update, :id => system_picture.id, :system_picture => {}
+        response.should render_template("edit")
+      end
+    end
+  end
+
+  describe "DELETE destroy" do
+    it "destroys the requested system_picture" do
+      system_picture = SystemPicture.create! valid_attributes
+      expect {
+        delete :destroy, :id => system_picture.id
+      }.to change(SystemPicture, :count).by(-1)
+    end
+
+    it "redirects to the system_pictures list" do
+      system_picture = SystemPicture.create! valid_attributes
+      delete :destroy, :id => system_picture.id
+      response.should redirect_to(system_pictures_url)
+    end
+  end
+
+end
diff --git a/spec/helpers/system_pictures_helper_spec.rb b/spec/helpers/system_pictures_helper_spec.rb
new file mode 100644 (file)
index 0000000..839a2e7
--- /dev/null
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+# Specs in this file have access to a helper object that includes
+# the SystemPicturesHelper. For example:
+#
+# describe SystemPicturesHelper do
+#   describe "string concat" do
+#     it "concats two strings with spaces" do
+#       helper.concat_strings("this","that").should == "this that"
+#     end
+#   end
+# end
+describe SystemPicturesHelper do
+  pending "add some examples to (or delete) #{__FILE__}"
+end
diff --git a/spec/models/system_picture_spec.rb b/spec/models/system_picture_spec.rb
new file mode 100644 (file)
index 0000000..cc05684
--- /dev/null
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe SystemPicture do
+  pending "add some examples to (or delete) #{__FILE__}"
+end
diff --git a/spec/requests/system_pictures_spec.rb b/spec/requests/system_pictures_spec.rb
new file mode 100644 (file)
index 0000000..bc399e5
--- /dev/null
@@ -0,0 +1,11 @@
+require 'spec_helper'
+
+describe "SystemPictures" do
+  describe "GET /system_pictures" do
+    it "works! (now write some real specs)" do
+      # Run the generator again with the --webrat flag if you want to use webrat methods/matchers
+      get system_pictures_path
+      response.status.should be(200)
+    end
+  end
+end
diff --git a/spec/routing/system_pictures_routing_spec.rb b/spec/routing/system_pictures_routing_spec.rb
new file mode 100644 (file)
index 0000000..2239562
--- /dev/null
@@ -0,0 +1,35 @@
+require "spec_helper"
+
+describe SystemPicturesController do
+  describe "routing" do
+
+    it "routes to #index" do
+      get("/system_pictures").should route_to("system_pictures#index")
+    end
+
+    it "routes to #new" do
+      get("/system_pictures/new").should route_to("system_pictures#new")
+    end
+
+    it "routes to #show" do
+      get("/system_pictures/1").should route_to("system_pictures#show", :id => "1")
+    end
+
+    it "routes to #edit" do
+      get("/system_pictures/1/edit").should route_to("system_pictures#edit", :id => "1")
+    end
+
+    it "routes to #create" do
+      post("/system_pictures").should route_to("system_pictures#create")
+    end
+
+    it "routes to #update" do
+      put("/system_pictures/1").should route_to("system_pictures#update", :id => "1")
+    end
+
+    it "routes to #destroy" do
+      delete("/system_pictures/1").should route_to("system_pictures#destroy", :id => "1")
+    end
+
+  end
+end
diff --git a/spec/views/system_pictures/edit.html.erb_spec.rb b/spec/views/system_pictures/edit.html.erb_spec.rb
new file mode 100644 (file)
index 0000000..0466c8d
--- /dev/null
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+describe "system_pictures/edit.html.erb" do
+  before(:each) do
+    @system_picture = assign(:system_picture, stub_model(SystemPicture))
+  end
+
+  it "renders the edit system_picture form" do
+    render
+
+    # Run the generator again with the --webrat flag if you want to use webrat matchers
+    assert_select "form", :action => system_pictures_path(@system_picture), :method => "post" do
+    end
+  end
+end
diff --git a/spec/views/system_pictures/index.html.erb_spec.rb b/spec/views/system_pictures/index.html.erb_spec.rb
new file mode 100644 (file)
index 0000000..54cad60
--- /dev/null
@@ -0,0 +1,14 @@
+require 'spec_helper'
+
+describe "system_pictures/index.html.erb" do
+  before(:each) do
+    assign(:system_pictures, [
+      stub_model(SystemPicture),
+      stub_model(SystemPicture)
+    ])
+  end
+
+  it "renders a list of system_pictures" do
+    render
+  end
+end
diff --git a/spec/views/system_pictures/new.html.erb_spec.rb b/spec/views/system_pictures/new.html.erb_spec.rb
new file mode 100644 (file)
index 0000000..09edd79
--- /dev/null
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+describe "system_pictures/new.html.erb" do
+  before(:each) do
+    assign(:system_picture, stub_model(SystemPicture).as_new_record)
+  end
+
+  it "renders new system_picture form" do
+    render
+
+    # Run the generator again with the --webrat flag if you want to use webrat matchers
+    assert_select "form", :action => system_pictures_path, :method => "post" do
+    end
+  end
+end
diff --git a/spec/views/system_pictures/show.html.erb_spec.rb b/spec/views/system_pictures/show.html.erb_spec.rb
new file mode 100644 (file)
index 0000000..eebc298
--- /dev/null
@@ -0,0 +1,11 @@
+require 'spec_helper'
+
+describe "system_pictures/show.html.erb" do
+  before(:each) do
+    @system_picture = assign(:system_picture, stub_model(SystemPicture))
+  end
+
+  it "renders attributes in <p>" do
+    render
+  end
+end