Class: ProductTours::PostsController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- ProductTours::PostsController
- Defined in:
- app/controllers/product_tours/posts_controller.rb
Constant Summary collapse
- PER_PAGE =
50
Instance Method Summary collapse
- #add_translation ⇒ Object
- #create ⇒ Object
- #destroy ⇒ Object
- #edit ⇒ Object
- #index ⇒ Object
- #new ⇒ Object
- #publish ⇒ Object
- #refresh_video_metadata ⇒ Object
- #show ⇒ Object
- #unpublish ⇒ Object
- #update ⇒ Object
- #video_preview ⇒ Object
Instance Method Details
#add_translation ⇒ Object
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 106 107 |
# File 'app/controllers/product_tours/posts_controller.rb', line 73 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 |
#create ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'app/controllers/product_tours/posts_controller.rb', line 39 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 |
#destroy ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'app/controllers/product_tours/posts_controller.rb', line 59 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 |
#edit ⇒ Object
48 |
# File 'app/controllers/product_tours/posts_controller.rb', line 48 def edit; end |
#index ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/controllers/product_tours/posts_controller.rb', line 14 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 |
#new ⇒ Object
35 36 37 |
# File 'app/controllers/product_tours/posts_controller.rb', line 35 def new @post = Post.new(locale: default_locale) end |
#publish ⇒ Object
109 110 111 112 |
# File 'app/controllers/product_tours/posts_controller.rb', line 109 def publish @post.update!(status: 'published') redirect_to post_path(@post), notice: t('product_tours.dashboard.published', default: 'Tutorial published.') end |
#refresh_video_metadata ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'app/controllers/product_tours/posts_controller.rb', line 138 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 |
#show ⇒ Object
33 |
# File 'app/controllers/product_tours/posts_controller.rb', line 33 def show; end |
#unpublish ⇒ Object
114 115 116 117 118 |
# File 'app/controllers/product_tours/posts_controller.rb', line 114 def unpublish @post.update!(status: 'draft') redirect_to post_path(@post), notice: t('product_tours.dashboard.unpublished', default: 'Tutorial moved to drafts.') end |
#update ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'app/controllers/product_tours/posts_controller.rb', line 50 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_preview ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'app/controllers/product_tours/posts_controller.rb', line 120 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 |