Class: AdSpace::AdvertisementsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/ad_space/advertisements_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#_current_user, #add_breadcrumb, #breadcrumbs, #init_app

Instance Method Details

#archiveObject



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/controllers/ad_space/advertisements_controller.rb', line 96

def archive
  unless @advertisement.archived?
    @advertisement.update(archived_at: Time.zone.now)
  else
    @advertisement.update(archived_at: nil)
  end

  render turbo_stream: turbo_stream.update(
    view_context.dom_id(@advertisement),
    partial: "advertisement",
    locals: { advertisement: @advertisement }
  )
end

#createObject

POST /advertisements



31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/ad_space/advertisements_controller.rb', line 31

def create
  create_params = advertisement_params.to_hash.deep_symbolize_keys
  create_params[:target_version_codes] = create_params[:target_version_codes].split(',').map(&:strip).map(&:to_i).uniq.compact
  @advertisement = Advertisement.new(create_params)

  if @advertisement.save
    redirect_to @advertisement, notice: "Advertisement was successfully created."
  else
    render :new, status: :unprocessable_entity
  end
end

#destroyObject

DELETE /advertisements/1



56
57
58
59
# File 'app/controllers/ad_space/advertisements_controller.rb', line 56

def destroy
  @advertisement.destroy!
  redirect_to advertisements_url, notice: "Advertisement was successfully destroyed.", status: :see_other
end

#editObject

GET /advertisements/1/edit



26
27
28
# File 'app/controllers/ad_space/advertisements_controller.rb', line 26

def edit
  add_breadcrumb(t("home"), root_path)
end

#indexObject

GET /advertisements



6
7
8
9
10
# File 'app/controllers/ad_space/advertisements_controller.rb', line 6

def index
  @advertisements = Advertisement.all

  add_breadcrumb(t("home"), root_path)
end

#newObject

GET /advertisements/new



19
20
21
22
23
# File 'app/controllers/ad_space/advertisements_controller.rb', line 19

def new
  @advertisement = Advertisement.new

  add_breadcrumb(t("home"), root_path)
end

#publishObject



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/ad_space/advertisements_controller.rb', line 82

def publish
  unless @advertisement.published?
    @advertisement.update(published_at: Time.zone.now)
  else
    @advertisement.update(published_at: nil)
  end

  render turbo_stream: turbo_stream.update(
    view_context.dom_id(@advertisement),
    partial: "advertisement",
    locals: { advertisement: @advertisement }
  )
end

#remove_coverObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/ad_space/advertisements_controller.rb', line 61

def remove_cover
  size = params[:size]

  # 根据传递的参数来决定要删除哪个尺寸的封面图片
  case size
  when "sm"
    @advertisement.cover_sm.purge if @advertisement.cover_sm.attached?
  when "base"
    @advertisement.cover_base.purge if @advertisement.cover_base.attached?
  when "md"
    @advertisement.cover_md.purge if @advertisement.cover_md.attached?
  when "lg"
    @advertisement.cover_lg.purge if @advertisement.cover_lg.attached?
  else
    render json: { error: "Invalid size parameter" }, status: :unprocessable_entity
    return
  end

  render json: { success: true }
end

#showObject

GET /advertisements/1



13
14
15
16
# File 'app/controllers/ad_space/advertisements_controller.rb', line 13

def show
  add_breadcrumb(t("home"), root_path)
  add_breadcrumb(t("advertisements"), advertisements_path)
end

#updateObject

PATCH/PUT /advertisements/1



44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/ad_space/advertisements_controller.rb', line 44

def update
  update_params = advertisement_params.to_hash.deep_symbolize_keys
  update_params[:target_version_codes] = update_params[:target_version_codes].split(',').map(&:strip).map(&:to_i).uniq.compact

  if @advertisement.update(update_params)
    redirect_to @advertisement, notice: "Advertisement was successfully updated.", status: :see_other
  else
    render :edit, status: :unprocessable_entity
  end
end