bigdir/app/controllers/softapplications_controller.rb

228 lines
6.5 KiB
Ruby

class SoftapplicationsController < ApplicationController
before_filter :find_softapplication, only: [:edit, :update, :destroy]
before_filter :editable, only: [:edit, :update]
before_filter :destroyable, only: :destroy
# GET /softapplications
# GET /softapplications.json
def index
@softapplications = Softapplication.all
@limit = 5
#new added
@softapplication_count = @softapplications.count
@softapplication_pages = Paginator.new @softapplication_count, @limit, params['page']
@offset ||= @softapplication_pages.offset
@softapplications = @softapplications[@offset,@limit]
#new added end
#@softapplications = @softapplications[@offset,@limit]
respond_to do |format|
format.html # index.html.erb
format.json { render json: @softapplications }
end
end
# GET /softapplications/1
# GET /softapplications/1.json
def show
@softapplication = Softapplication.find(params[:id])
@jours = @softapplication.journals_for_messages.order('created_on DESC')
@image_results = []
@softapplication.attachments.each do |f|
f.image? ? @image_results << f : @image_results
end
@app_items = []
@softapplication.attachments.each do |f|
f.pack? ? @app_items << f : @app_items
end
@limit = 10
@feedback_count = @jours.count
@feedback_pages = Paginator.new @feedback_count, @limit, params['page']
@offset ||= @feedback_pages.offset
@jour = @jours[@offset, @limit]
@state = false
respond_to do |format|
format.html # show.html.erb
format.json { render json: @softapplication }
end
end
# GET /softapplications/new
# GET /softapplications/new.json
def new
@softapplication = Softapplication.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @softapplication }
end
end
# GET /softapplications/1/edit
def edit
@softapplication = Softapplication.find(params[:id])
end
# POST /softapplications
# POST /softapplications.json
def create
@softapplication = Softapplication.new(params[:softapplication])
@softapplication.user = User.current
@softapplication.save_attachments(params[:attachments])
respond_to do |format|
if @softapplication.save
format.html { redirect_to @softapplication, notice: 'Softapplication was successfully created.' }
format.json { render json: @softapplication, status: :created, location: @softapplication }
else
format.html { render action: "new" }
format.json { render json: @softapplication.errors, status: :unprocessable_entity }
end
end
end
# PUT /softapplications/1
# PUT /softapplications/1.json
def update
# @softapplication = Softapplication.find(params[:id])
@softapplication.attachments.map{|attach| attach.destroy }
@softapplication.save_attachments(params[:attachments])
respond_to do |format|
if @softapplication.update_attributes(params[:softapplication])
format.html { redirect_to @softapplication, notice: 'Softapplication was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @softapplication.errors, status: :unprocessable_entity }
end
end
end
def add_attach
@softapplication = Softapplication.find(params[:id])
@softapplication.save_attachments(params[:attachments])
end
# DELETE /softapplications/1
# DELETE /softapplications/1.json
def destroy
# @softapplication = Softapplication.find(params[:id])
@softapplication.destroy
respond_to do |format|
format.html { redirect_to home_path }
format.json { head :no_content }
end
end
#应用评价涉及到的方法
def new_message
@jour = JournalsForMessage.find(params[:journal_id]) if params[:journal_id]
if @jour
user = @jour.user
text = @jour.notes
else
user = @softapplication.user
text = @softapplication.description
end
text = text.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]')
@content = "> #{ll(User.current.language, :text_user_wrote, user)}\n> "
@content << text.gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n"
@id = user.id
rescue ActiveRecord::RecordNotFound
render_404
end
#新建评价
def create_message
if params[:reference_content]
message = params[:softapplication_message][:message] + "\n" + params[:reference_content]
else
message = params[:softapplication_message][:message]
end
refer_user_id = params[:softapplication_message][:reference_user_id].to_i
@softapplication = Softapplication.find(params[:id])
@softapplication.add_jour(User.current, message, refer_user_id)
@user = @softapplication.user
@jours = @softapplication.journals_for_messages.where('m_parent_id IS NULL').order('created_on DESC')
@limit = 10
@feedback_count = @jours.count
@feedback_pages = Paginator.new @feedback_count, @limit, params['page']
@offset ||= @feedback_pages.offset
@jour = @jours[@offset, @limit]
#@softapplication.set_commit(@feedback_count)
respond_to do |format|
format.js
end
end
##删除评价
def destroy_message
@user = @softapplication.user
if User.current.admin? || User.current.id == @user.id
JournalsForMessage.delete_message(params[:object_id])
end
@jours = @softapplication.journals_for_messages.reverse
@limit = 10
@feedback_count = @jours.count
@feedback_pages = Paginator.new @feedback_count, @limit, params['page']
@offset ||= @feedback_pages.offset
@jour = @jours[@offset, @limit]
@softapplication.set_commit(@feedback_count)
respond_to do |format|
format.js
end
end
#
def more
@jour = @softapplication.journals_for_messages
@jour.each_with_index {|j,i| j.indice = i+1}
@state = true
respond_to do |format|
format.html { redirect_to :back }
format.js
#format.api { render_api_ok }
end
end
#
def back
@jour = @softapplication.journals_for_messages
@jour.each_with_index {|j,i| j.indice = i+1}
@state = false
respond_to do |format|
format.html { redirect_to :back }
format.js
#format.api { render_api_ok }
end
end
private
def find_softapplication
@softapplication = Softapplication.find_by_id(params[:id])
end
def editable
unless @softapplication.editable_by? User.current
render_403
return false
end
end
def destroyable
unless @softapplication.destroyable_by? User.current
render_403
return false
end
end
end