Class: NewsController

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

Overview

This controller scopes to News::Project classes for show and index.

See the related Administration controller for Administration news scoping of show and index.

Create/udpate scoping to Administrators is handled in the model.

Direct Known Subclasses

News::AdministrationController

Constant Summary

Constants included from ProjectsHelper

ProjectsHelper::CLASSIFIER, ProjectsHelper::CLASSIFIER_ANNOTATION

Instance Method Summary collapse

Methods included from RedirectHelper

#destroy_redirect

Methods included from RequestType

#json_request?

Methods included from LogRecent

#log_user_recent_route

Methods included from Cookies

#digest_cookie, #digested_cookie_exists?

Methods included from Whitelist

#whitelist_constantize

Methods included from ProjectsHelper

#cumulative_gb_per_year, #cumulative_projects_created_per_year, #document_cumulative_gb_per_year, #document_gb_per_year, #gb_per_year, #image_cumulative_gb_per_year, #image_gb_per_year, #invalid_object, #project_classification, #project_initials, #project_link, #project_login_link, #project_matches, #project_tag, #projects_list, #projects_search_form, #sound_cumulative_gb_per_year, #sound_gb_per_year, #taxonworks_classification, #week_in_review_graphs

Methods included from Api::Intercept

#intercept_api

Methods included from TokenAuthentication

#intercept_project, #intercept_user, #intercept_user_or_project, #project_token_authenticate, #token_authenticate

Instance Method Details

#api_indexObject

Serves only current blog_posts at the moment



31
32
33
34
35
36
37
38
39
# File 'app/controllers/news_controller.rb', line 31

def api_index
  @news = News::Project::BlogPost
    .where(project_id: sessions_current_project_id)
    .page(params[:page])
    .per(params[:per])
    .order(:display_start, :created_at)

  render '/news/api/v1/index'
end

#api_showObject

Serves only current blog_posts at the moment



42
43
44
45
# File 'app/controllers/news_controller.rb', line 42

def api_show
  @news = News::Project::BlogPost.find(params[:id])
  render '/news/api/v1/show'
end

#createObject

POST /news or /news.json



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/news_controller.rb', line 61

def create
  @news = News.new(news_params)

  respond_to do |format|
    if @news.save
      format.html { redirect_to @news.becomes(@news.class.base_class), notice: 'News was successfully created.' }
      format.json { render :show, status: :created, location: @news.metamorphosize }
    else
      format.html { render :new, status: :unprocessable_entity }
      format.json { render json: @news.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /news/1 or /news/1.json



89
90
91
92
93
94
95
96
# File 'app/controllers/news_controller.rb', line 89

def destroy
  @news.destroy!

  respond_to do |format|
    format.html { redirect_to news_index_path, notice: 'News was successfully destroyed.', status: :see_other }
    format.json { head :no_content }
  end
end

#editObject

GET /news/1/edit



57
58
# File 'app/controllers/news_controller.rb', line 57

def edit
end

#indexObject

GET /news or /news.json



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

def index

  respond_to do |format|
    # We use the "Newspaper" for displaying news
    format.html do
      redirect_to :browse_news_task  and return
    end
    format.json {
      @news = News::Project.where(project_id: sessions_current_project_id)
        .page(params[:page])
        .per(params[:per])
        .order(created_at: :desc)
    }
  end
end

#listObject



98
99
100
# File 'app/controllers/news_controller.rb', line 98

def list
  @news = News.where(project_id: sessions_current_project_id).page(params[:page]).per(params[:per])
end

#newObject

GET /news/new



52
53
54
# File 'app/controllers/news_controller.rb', line 52

def new
  @news = News.new
end

#news_paramsObject (private)



116
117
118
# File 'app/controllers/news_controller.rb', line 116

def news_params
  params.require(:news).permit(:type, :title, :body, :display_start, :display_end, :is_public)
end

#set_newsObject (private)



112
113
114
# File 'app/controllers/news_controller.rb', line 112

def set_news
  @news = News.find(params[:id])
end

#showObject

GET /news/1 or /news/1.json



48
49
# File 'app/controllers/news_controller.rb', line 48

def show
end

#typesObject



102
103
104
105
106
107
108
109
# File 'app/controllers/news_controller.rb', line 102

def types
  types = {
    project: News::PROJECT_TYPES.keys,
    administration: News::ADMINISTRATION_TYPES.keys
  }
 
  render json: types
end

#updateObject

PATCH/PUT /news/1 or /news/1.json



76
77
78
79
80
81
82
83
84
85
86
# File 'app/controllers/news_controller.rb', line 76

def update
  respond_to do |format|
    if @news.update(news_params)
      format.html { redirect_to @news.metamorphosize, notice: 'News was successfully updated.', status: :see_other }
      format.json { render :show, status: :ok, location: @news.metamorphosize }
    else
      format.html { render :edit, status: :unprocessable_entity }
      format.json { render json: @news.errors, status: :unprocessable_entity }
    end
  end
end