diff --git a/app/controllers/ajax/inbox_controller.rb b/app/controllers/ajax/inbox_controller.rb index dfffa89e..2f30106f 100644 --- a/app/controllers/ajax/inbox_controller.rb +++ b/app/controllers/ajax/inbox_controller.rb @@ -1,4 +1,26 @@ class Ajax::InboxController < ApplicationController + def create + unless user_signed_in? + @status = :noauth + @message = "requires authentication" + @success = false + return + end + + question = Question.create!(content: QuestionGenerator.generate, + author_is_anonymous: true, + author_name: 'justask', + user: current_user) + + inbox = Inbox.create!(user: current_user, question_id: question.id, new: true) + + @status = :okay + @message = "Successfully added new question." + @success = true + @render = render_to_string(partial: 'inbox/entry', locals: { i: inbox }) + inbox.update(new: false) + end + def destroy params.require :id params.require :answer diff --git a/app/views/ajax/inbox/create.json.jbuilder b/app/views/ajax/inbox/create.json.jbuilder new file mode 100644 index 00000000..2c193af4 --- /dev/null +++ b/app/views/ajax/inbox/create.json.jbuilder @@ -0,0 +1 @@ +json.partial! 'ajax/shared/status' \ No newline at end of file diff --git a/app/views/ajax/question/create.json.jbuilder b/app/views/ajax/question/create.json.jbuilder index 2c193af4..f2c8c8c4 100644 --- a/app/views/ajax/question/create.json.jbuilder +++ b/app/views/ajax/question/create.json.jbuilder @@ -1 +1,2 @@ -json.partial! 'ajax/shared/status' \ No newline at end of file +json.partial! 'ajax/shared/status' +json.render @render \ No newline at end of file diff --git a/app/views/inbox/_entry.html.haml b/app/views/inbox/_entry.html.haml new file mode 100644 index 00000000..a4f87f37 --- /dev/null +++ b/app/views/inbox/_entry.html.haml @@ -0,0 +1,25 @@ +.panel.inbox-box{class: "panel-#{i.new? ? 'primary' : 'default'}", data: { id: i.id }} + .panel-heading + .media + - unless i.question.author_is_anonymous + %a.pull-left{href: show_user_profile_path(i.question.user.screen_name)} + %img.img-rounded.answerbox--img{src: gravatar_url(i.question.user)} + .media-body + %h6.text-muted.media-heading.answerbox--question-user + = user_screen_name i.question.user, i.question.author_is_anonymous + asked + = time_ago_in_words(i.question.created_at) + ago + - unless i.question.author_is_anonymous + - if i.question.answer_count > 0 + · + %a{href: show_user_question_path(i.question.user.screen_name, i.question.id)} + #{i.question.answer_count} response(s) + %p.answerbox--question-text= i.question.content + .panel-body + %textarea.form-control{name: 'ib-answer', data: { id: i.id }} + %br/ + %button.btn.btn-success{name: 'ib-answer', data: { ib_id: i.id }} + Answer + %button.btn.btn-danger{name: 'ib-destroy', data: { ib_id: i.id }} + Delete \ No newline at end of file diff --git a/app/views/inbox/show.html.haml b/app/views/inbox/show.html.haml index ded7977a..aa224aab 100644 --- a/app/views/inbox/show.html.haml +++ b/app/views/inbox/show.html.haml @@ -1,31 +1,8 @@ .container.j2-page = render 'layouts/messages' - - @inbox.each do |i| - .panel.inbox-box{class: "panel-#{i.new? ? 'primary' : 'default'}", data: { id: i.id }} - .panel-heading - .media - - unless i.question.author_is_anonymous - %a.pull-left{href: show_user_profile_path(i.question.user.screen_name)} - %img.img-rounded.answerbox--img{src: gravatar_url(i.question.user)} - .media-body - %h6.text-muted.media-heading.answerbox--question-user - = user_screen_name i.question.user, i.question.author_is_anonymous - asked - = time_ago_in_words(i.question.created_at) - ago - - unless i.question.author_is_anonymous - - if i.question.answer_count > 0 - · - %a{href: show_user_question_path(i.question.user.screen_name, i.question.id)} - #{i.question.answer_count} response(s) - %p.answerbox--question-text= i.question.content - .panel-body - %textarea.form-control{name: 'ib-answer', data: { id: i.id }} - %br/ - %button.btn.btn-success{name: 'ib-answer', data: { ib_id: i.id }} - Answer - %button.btn.btn-danger{name: 'ib-destroy', data: { ib_id: i.id }} - Delete + #entries + - @inbox.each do |i| + = render 'inbox/entry', i: i - if @inbox.empty? diff --git a/config/routes.rb b/config/routes.rb index 447687c5..a3fff9dc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -25,10 +25,11 @@ Rails.application.routes.draw do match '/settings/profile', to: 'user#edit', via: 'get', as: :edit_user_profile match '/settings/profile', to: 'user#update', via: 'patch', as: :update_user_profile - + namespace :ajax do match '/ask', to: 'question#create', via: :post, as: :ask match '/answer', to: 'inbox#destroy', via: :post, as: :answer + match '/generate_question', to: 'inbox#create', via: :post, as: :generate_question match '/delete_inbox', to: 'inbox#remove', via: :post, as: :delete_inbox match '/destroy_answer', to: 'answer#destroy', via: :post, as: :destroy_answer match '/create_friend', to: 'friend#create', via: :post, as: :create_friend