Class: Darjeelink::ShortLinksController

Inherits:
ApplicationController show all
Defined in:
app/controllers/darjeelink/short_links_controller.rb

Defined Under Namespace

Classes: ShortLinkNotFoundError

Instance Method Summary collapse

Instance Method Details

#createObject



24
25
26
27
28
# File 'app/controllers/darjeelink/short_links_controller.rb', line 24

def create
  handle_create_or_update(error_redirect_path: darjeelink.new_short_link_path) do
    Darjeelink::ShortLink.create!(short_link_params)
  end
end

#editObject



30
31
32
# File 'app/controllers/darjeelink/short_links_controller.rb', line 30

def edit
  @short_link = Darjeelink::ShortLink.find(params[:id])
end

#indexObject



14
15
16
17
18
# File 'app/controllers/darjeelink/short_links_controller.rb', line 14

def index
  @short_links = Darjeelink::ShortLink.search(params[:query])
                                      .order(id: 'desc')
                                      .paginate(page: params[:page], per_page: 20)
end

#newObject



20
21
22
# File 'app/controllers/darjeelink/short_links_controller.rb', line 20

def new
  @short_link = Darjeelink::ShortLink.new
end

#showObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/darjeelink/short_links_controller.rb', line 42

def show
  shortened_path = CGI.escape(params[:id])
  short_link = Darjeelink::ShortLink.ifind(shortened_path)

  if short_link.nil?
    log_missing_shortlink(shortened_path)
    return redirect_to(Darjeelink.fallback_url)
  end

  short_link.update!(visits: short_link.visits + 1)
  redirect_to(build_url(short_link.url))
end

#updateObject



34
35
36
37
38
39
40
# File 'app/controllers/darjeelink/short_links_controller.rb', line 34

def update
  @short_link = Darjeelink::ShortLink.find(params[:id])

  handle_create_or_update(error_redirect_path: darjeelink.edit_short_link_path(@short_link)) do
    @short_link.update!(short_link_params)
  end
end