Class: Collavre::UserThemesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/collavre/user_themes_controller.rb

Instance Method Summary collapse

Instance Method Details

#applyObject



49
50
51
52
53
54
55
56
# File 'app/controllers/collavre/user_themes_controller.rb', line 49

def apply
  @theme = Current.user.user_themes.find(params[:id])
  if Current.user.update(theme: @theme.id.to_s)
    redirect_to user_themes_path, notice: t("collavre.themes.alerts.applied")
  else
    redirect_to user_themes_path, alert: t("collavre.themes.alerts.apply_failed", errors: Current.user.errors.full_messages.join(", "))
  end
end

#createObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/collavre/user_themes_controller.rb', line 8

def create
  description = params[:description]
  if description.blank?
    @error = t("collavre.themes.alerts.description_empty")
    @themes = Current.user.user_themes.order(created_at: :desc)
    @new_theme = UserTheme.new
    render :index
    return
  end

  variables = AutoThemeGenerator.new.generate(description)

  if variables.empty?
    @error = t("collavre.themes.alerts.generation_failed")
    @themes = Current.user.user_themes.order(created_at: :desc)
    @new_theme = UserTheme.new
    render :index
    return
  end

  # Generate a name from description or use first few words
  name = description.truncate(30)

  @theme = Current.user.user_themes.build(name: name, variables: variables)

  if @theme.save
    redirect_to user_themes_path, notice: t("collavre.themes.alerts.success")
  else
    redirect_to user_themes_path, alert: t("collavre.themes.alerts.save_failed")
  end
end

#destroyObject



40
41
42
43
44
45
46
47
# File 'app/controllers/collavre/user_themes_controller.rb', line 40

def destroy
  @theme = Current.user.user_themes.find(params[:id])
  @theme.destroy
  if Current.user.theme == @theme.id.to_s
    Current.user.update(theme: "light")
  end
  redirect_to user_themes_path, notice: t("collavre.themes.alerts.deleted")
end

#indexObject



3
4
5
6
# File 'app/controllers/collavre/user_themes_controller.rb', line 3

def index
  @themes = Current.user.user_themes.order(created_at: :desc)
  @new_theme = UserTheme.new
end