Class: ShortUrlsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/short_urls_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /short_urls POST /short_urls.json



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/short_urls_controller.rb', line 53

def create
  @short_url = ShortUrl.new(params[:short_url])

  respond_to do |format|
    if @short_url.save
      format.html { redirect_to @short_url, notice: 'Short url was successfully created.' }
      format.json { render json: @short_url, status: :created, location: @short_url }
    else
      format.html { render action: "new" }
      format.json { render json: @short_url.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /short_urls/1 DELETE /short_urls/1.json



85
86
87
88
89
90
91
92
93
# File 'app/controllers/short_urls_controller.rb', line 85

def destroy
  @short_url = ShortUrl.find(params[:id])
  @short_url.destroy

  respond_to do |format|
    format.html { redirect_to short_urls_url }
    format.json { head :no_content }
  end
end

#editObject

GET /short_urls/1/edit



47
48
49
# File 'app/controllers/short_urls_controller.rb', line 47

def edit
  @short_url = ShortUrl.find(params[:id])
end

#expandObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/short_urls_controller.rb', line 95

def expand
  @short_url = ShortUrl.find_by_code(params[:code])

  respond_to do |format|
    if @short_url
      @exp = @short_url.expansions.build
      @exp.referrer_url = request.env['HTTP_REFERER']
      @exp.request_url  = request.original_url
      @exp.user_agent   = request.env["HTTP_USER_AGENT"]
      @exp.remote_ip    = request.remote_ip
      @exp.remote_addr  = request.remote_ip
      @exp.save
      @short_url.increment!(:expansion_count)

      format.html { redirect_to @short_url.destination_url }
    else
      format.html { redirect_to invalid_code_path }
    end
  end
end

#indexObject

GET /short_urls GET /short_urls.json



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/controllers/short_urls_controller.rb', line 5

def index
  @short_domain = ShortDomain.find_by_id(params[:short_domain_id])
  @short_urls = if @short_domain
    @short_domain.short_urls.includes(:short_domain)
  else
    ShortUrl.includes(:short_domain)
  end

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @short_urls }
  end
end

#newObject

GET /short_urls/new GET /short_urls/new.json



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/short_urls_controller.rb', line 32

def new
  @short_domain = ShortDomain.find_by_id(params[:short_domain_id], :include => [:short_urls])
  @short_url = if @short_domain
    @short_domain.short_urls.build
  else
    ShortUrl.new
  end
  
  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @short_url }
  end
end

#showObject

GET /short_urls/1 GET /short_urls/1.json



21
22
23
24
25
26
27
28
# File 'app/controllers/short_urls_controller.rb', line 21

def show
  @short_url = ShortUrl.find(params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @short_url }
  end
end

#updateObject

PUT /short_urls/1 PUT /short_urls/1.json



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/short_urls_controller.rb', line 69

def update
  @short_url = ShortUrl.find(params[:id])

  respond_to do |format|
    if @short_url.update_attributes(params[:short_url])
      format.html { redirect_to @short_url, notice: 'Short url was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: "edit" }
      format.json { render json: @short_url.errors, status: :unprocessable_entity }
    end
  end
end