94 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Ruby
		
	
	
	
			
		
		
	
	
			94 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Ruby
		
	
	
	
| class Admin::LinksController < Admin::ItemsController
 | |
|   
 | |
|   def show
 | |
|     @item ||= Link.find(params[:id])
 | |
|   end
 | |
|   
 | |
|   def new
 | |
|     @item = Link.new
 | |
|     @item.parent = Page.find(params[:parent_id]) rescue nil
 | |
|     render layout: false
 | |
|   end
 | |
| 
 | |
|   def edit
 | |
|     @item = Link.find(params[:id])
 | |
|     render layout: false
 | |
|   end
 | |
| 
 | |
|   def create
 | |
|     @item = Link.new(params[:link])
 | |
| 
 | |
|     if @item.save(params[:link])
 | |
|       success = true
 | |
|     else
 | |
|       success = check_valid_url
 | |
|     end
 | |
|     if success
 | |
|       flash.now[:notice] = t('create.success.link')
 | |
|       respond_to do |format|
 | |
|         format.js { render 'admin/items/reload_items' }
 | |
|       end
 | |
|     else
 | |
|       flash.now[:error] = t('create.error.link')
 | |
|       render :action => "new"
 | |
|     end
 | |
|   end
 | |
| 
 | |
|   def update
 | |
|     @item = Link.find(params[:id])
 | |
| 
 | |
|     if @item.update_attributes(params[:link])
 | |
|       success = true
 | |
|     else
 | |
|       success = check_valid_url
 | |
|     end
 | |
|     if success
 | |
|       flash.now[:notice] = t('update.success.link')
 | |
|       respond_to do |format|
 | |
|         format.js { render 'admin/items/reload_items' }
 | |
|       end
 | |
|     else
 | |
|       flash.now[:error] = t('update.error.link')
 | |
|       render :action => "edit"
 | |
|     end
 | |
|   end
 | |
| 
 | |
|   protected
 | |
| 
 | |
|   def check_valid_url
 | |
|     if @item.errors.include?(:url) && !@item.errors.added?(:url, :blank) && @item.errors.added?(:url, :invalid)
 | |
|       begin
 | |
|         url = @item.url
 | |
|         url.gsub!('http://', '').slice!(/^\//)
 | |
|         path = Rails.application.routes.recognize_path(url)
 | |
|         if path.has_key?(:page_name)
 | |
|           if Page.where(path: path[:page_name]).first
 | |
|             new_url = "#{request.base_url}/#{url}"
 | |
|           else
 | |
|             success = false
 | |
|           end
 | |
|         else
 | |
|           new_url = "#{request.base_url}/#{url}"
 | |
|         end
 | |
|         if @item.errors.count == 1
 | |
|           @item.url = new_url
 | |
|           if @item.save
 | |
|             success = true
 | |
|           else
 | |
|             success = false
 | |
|           end
 | |
|         else
 | |
|           @item.url = new_url
 | |
|           success = false
 | |
|         end unless success == false
 | |
|       rescue
 | |
|         success = false
 | |
|       end
 | |
|     else
 | |
|       success = false
 | |
|     end
 | |
|     success
 | |
|   end
 | |
|   
 | |
| end
 |