Class: ProductTours::PostsController

Inherits:
DashboardController show all
Defined in:
app/controllers/product_tours/posts_controller.rb

Constant Summary collapse

PER_PAGE =
50

Instance Method Summary collapse

Instance Method Details

#add_translationObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/product_tours/posts_controller.rb', line 71

def add_translation
  locale = params[:locale].to_s
  unless available_locales.include?(locale) && locale != @post.locale
    redirect_to post_path(@post),
                alert: t('product_tours.dashboard.translation_locale_invalid',
                         default: 'Choose another available language.')
    return
  end

  existing = Post.find_by(locale: locale, key: @post.key)
  if existing
    redirect_to post_path(existing),
                notice: t('product_tours.dashboard.translation_exists',
                          default: 'That translation already exists.')
    return
  end

  source = translations_for(@post).find_by(locale: default_locale) || @post
  translation = source.dup
  translation.locale = locale
  translation.status = 'draft'
  if translation.action_post_key.present? &&
     !Post.exists?(locale: locale, key: translation.action_post_key)
    translation.action_post_key = nil
  end
  translation.save!
  if Post.description_supported? && source.description.present?
    translation.update!(description: source.description.body)
  end
  translation.video.attach(source.video.blob) if source.uploaded_video?

  redirect_to edit_post_path(translation),
              notice: t('product_tours.dashboard.translation_added',
                        default: 'Translation added as a draft. Translate its content before publishing.')
end

#createObject



37
38
39
40
41
42
43
44
# File 'app/controllers/product_tours/posts_controller.rb', line 37

def create
  @post = Post.new(post_attributes.merge(locale: default_locale))
  if @post.save
    redirect_to post_path(@post), notice: t('product_tours.dashboard.created', default: 'Tutorial created.')
  else
    render :new, status: :unprocessable_entity
  end
end

#destroyObject



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/product_tours/posts_controller.rb', line 57

def destroy
  if default_locale_post?(@post) && translations_for(@post).where.not(id: @post.id).exists?
    redirect_to post_path(@post),
                alert: t('product_tours.dashboard.delete_translations_first',
                         default: 'Delete this tutorial\'s translations before deleting the ' \
                                  'default-language tutorial.')
    return
  end

  @post.destroy!
  redirect_to posts_path,
              notice: t('product_tours.dashboard.deleted', default: 'Tutorial deleted.'), status: :see_other
end

#editObject



46
# File 'app/controllers/product_tours/posts_controller.rb', line 46

def edit; end

#indexObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/product_tours/posts_controller.rb', line 12

def index
  @status = Post::STATUSES.include?(params[:status]) ? params[:status] : 'published'
  @query = params[:q].to_s.strip.presence
  @counts = Post.where(locale: default_locale).group(:status).count

  key_scope = Post.where(status: @status, locale: default_locale)
  @keys = key_scope.distinct.order(:key).pluck(:key)

  scope = posts_with_video.newest_first.where(status: @status, locale: default_locale)
  scope = scope.where('LOWER(key) LIKE ?', "%#{Post.sanitize_sql_like(@query.downcase)}%") if @query
  @page = [params[:page].to_i, 1].max
  @posts = scope.offset((@page - 1) * PER_PAGE).limit(PER_PAGE + 1).to_a
  @more = @posts.size > PER_PAGE
  @posts = @posts.first(PER_PAGE)

  @selected_post = posts_with_video.find_by(id: params[:post_id]) if params[:post_id].present?
  load_translations if @selected_post
end

#newObject



33
34
35
# File 'app/controllers/product_tours/posts_controller.rb', line 33

def new
  @post = Post.new(locale: default_locale)
end

#publishObject



107
108
109
110
# File 'app/controllers/product_tours/posts_controller.rb', line 107

def publish
  @post.update!(status: 'published')
  redirect_to post_path(@post), notice: t('product_tours.dashboard.published', default: 'Tutorial published.')
end

#refresh_video_metadataObject



136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/controllers/product_tours/posts_controller.rb', line 136

def 
   = VideoMetadata.fetch(@post.video_url)
  if .present?
    @post.update!(video_metadata: @post..to_h.merge())
    redirect_to edit_post_path(@post),
                notice: t('product_tours.dashboard.metadata_updated', default: 'Video data refreshed.')
  else
    redirect_to edit_post_path(@post),
                alert: t('product_tours.dashboard.metadata_failed',
                         default: 'Video data could not be fetched. The URL was not changed.')
  end
end

#showObject



31
# File 'app/controllers/product_tours/posts_controller.rb', line 31

def show; end

#unpublishObject



112
113
114
115
116
# File 'app/controllers/product_tours/posts_controller.rb', line 112

def unpublish
  @post.update!(status: 'draft')
  redirect_to post_path(@post),
              notice: t('product_tours.dashboard.unpublished', default: 'Tutorial moved to drafts.')
end

#updateObject



48
49
50
51
52
53
54
55
# File 'app/controllers/product_tours/posts_controller.rb', line 48

def update
  if @post.update(post_attributes)
    remove_uploaded_video if remove_uploaded_video?
    redirect_to post_path(@post), notice: t('product_tours.dashboard.updated', default: 'Tutorial updated.')
  else
    render :edit, status: :unprocessable_entity
  end
end

#video_previewObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/controllers/product_tours/posts_controller.rb', line 118

def video_preview
  resolved = VideoResolver.resolve(params[:url])
  unless resolved
    render json: { error: t('product_tours.dashboard.video_invalid', default: 'Enter a supported video URL.') },
           status: :unprocessable_entity
    return
  end

   = VideoMetadata.fetch(params[:url])
  render json: {
    kind: resolved.kind,
    provider: resolved.provider,
    url: resolved.url,
    title: ['provider_title'],
    thumbnail_url: ['thumbnail_url']
  }.compact
end