Class: Goldencobra::Api::V2::ArticlesController
- Inherits:
-
ActionController::Base
- Object
- ActionController::Base
- Goldencobra::Api::V2::ArticlesController
- Defined in:
- app/controllers/goldencobra/api/v2/articles_controller.rb
Instance Method Summary collapse
- #breadcrumb ⇒ Object
-
#create ⇒ Object
/api/v2/articles/create ———————————————————————-.
- #index ⇒ json
- #index_with_ids ⇒ json
-
#search ⇒ Object
/api/v2/articles/search ———————————————————————-.
- #show ⇒ json
- #update ⇒ Object
Instance Method Details
#breadcrumb ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'app/controllers/goldencobra/api/v2/articles_controller.rb', line 157 def = [] @article.path.each do |art| << { title: art., url: art.public_url } end respond_to do |format| format.json { render json: .to_json } end end |
#create ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'app/controllers/goldencobra/api/v2/articles_controller.rb', line 124 def create if existing_articles.any? render status: 423, json: { status: 423, error: "article already exists", id: existing_articles.first.id } and return end # Try to save the article response = create_article(params[:article]) if response.id.present? render status: 200, json: { status: 200, id: response.id } else render status: 500, json: { status: 500, error: response.errors, id: nil } end end |
#index ⇒ json
map{ |c| [c.parent_path, c.id] }
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/controllers/goldencobra/api/v2/articles_controller.rb', line 39 def index if params[:article_ids].present? index_with_ids else @articles = cached_articles respond_to do |format| format.json do render json: Oj.dump( { articles: articles_as_json }, mode: :compat ) end # Returns all publicly visible, active Articles format.xml do @articles = Goldencobra::Article. new. ( Goldencobra::Article.active, nil ) end end end end |
#index_with_ids ⇒ json
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'app/controllers/goldencobra/api/v2/articles_controller.rb', line 100 def index_with_ids article_ids = params[:article_ids] cache_key ||= ["indexarticles", article_ids] articles = Rails.cache.fetch(cache_key) do Goldencobra::Article.where("id IN (?)", article_ids) end respond_to do |format| format.json do if params[:methods].present? render json: Oj.dump( articles, each_serializer: Goldencobra::ArticleCustomSerializer, scope: params[:methods] ) else render json: Oj.dump(articles) end end end end |
#search ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'app/controllers/goldencobra/api/v2/articles_controller.rb', line 16 def search # Check if we have an argument. unless params[:q] render status: 200, json: { status: 200 } return end # Check if the query string contains something. if params[:q].length == 0 render status: 200, json: { status: 200 } else # Search and return the result array. render status: 200, json: Goldencobra::Article.simple_search( ActionController::Base.helpers.sanitize(params[:q]) ).to_json end end |
#show ⇒ json
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'app/controllers/goldencobra/api/v2/articles_controller.rb', line 73 def show respond_to do |format| format.json do if params[:methods].present? render json: Oj.dump( @article.as_json, serializer: Goldencobra::ArticleCustomSerializer, scope: params[:methods] ) else render json: Oj.dump(@article.as_json) end end end end |
#update ⇒ Object
147 148 149 150 151 152 153 154 155 |
# File 'app/controllers/goldencobra/api/v2/articles_controller.rb', line 147 def update if @article.update_attributes(params[:article]) render status: 200, json: { status: 200, id: @article.id } # Erst render Ergebnis dann den Rest machen create_images(@article, params[:images]) else render_error(response.errors, 500) end end |