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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/news_controller.rb', line 32

def api_index
  @news = ::Queries::News::Filter.new(params)
    .all
    .current
    .where(
      type: News::Project::BlogPost.name, 
      is_public: true
    )
    .order(created_at: :desc)
    .page(params[:page])
    .per(params[:per])

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

#api_showObject

Serves only current blog_posts at the moment



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

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

#createObject

POST /news or /news.json



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/news_controller.rb', line 67

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_content }
      format.json { render json: @news.errors, status: :unprocessable_content }
    end
  end
end

#destroyObject

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



95
96
97
98
99
100
101
102
# File 'app/controllers/news_controller.rb', line 95

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



63
64
# File 'app/controllers/news_controller.rb', line 63

def edit
end

#indexObject

GET /news or /news.json



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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)
        .order(created_at: :desc)
        .page(params[:page])
        .per(params[:per])
        
    }
  end
end

#listObject



104
105
106
# File 'app/controllers/news_controller.rb', line 104

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

#newObject

GET /news/new



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

def new
  @news = News.new
end

#news_paramsObject (private)



122
123
124
# File 'app/controllers/news_controller.rb', line 122

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

#set_newsObject (private)



118
119
120
# File 'app/controllers/news_controller.rb', line 118

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

#showObject

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



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

def show
end

#typesObject



108
109
110
111
112
113
114
115
# File 'app/controllers/news_controller.rb', line 108

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



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

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_content }
      format.json { render json: @news.errors, status: :unprocessable_content }
    end
  end
end