Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

request headers #8

Merged
merged 23 commits into from
Aug 6, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/assets/javascripts/routes.js.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= JsRoutes.generate(default_url_options: { :host => ENV['APP_DOMAIN'] }, url_links: true, exclude: [/admin/] ) %>
<%= JsRoutes.generate(default_url_options: { :host => 'localhost:3000' }, url_links: true, exclude: [/admin/] ) %>
8 changes: 8 additions & 0 deletions app/controllers/api/v1/api_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Api::V1::ApiController < ActionController::Base
include DeviseTokenAuth::Concerns::SetUserByToken
# before_action :configure_permitted_parameters, if: :devise_controller?
before_filter :set_websites_from_header
layout nil
before_action :authenticate_member!

Expand All @@ -11,6 +12,13 @@ class Api::V1::ApiController < ActionController::Base
render json: {:alert => exception.message}
end

def set_websites_from_header
app_key = request.headers['HTTP_APP_KEY']
app_id = request.headers['HTTP_APP_ID']
@current_site ||= Website.find_by_app_id_and_app_key(app_id, app_key)
binding.pry
end

def current_site
if params[:website_id] && current_member
@current_site ||= current_member.websites.where("websites.id = ?", params[:website_id]).try(:first)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hai sa facem aici altfel. Hai sa combinam in metoda asta current_site cu metoda aia set_websites_from_header. Fai aici asa:

if params[:website_id] && current_member
   @current_site ||= current_member.websites.where("websites.id = ?", params[:website_id]).try(:first)
else
   @current_site ||= Website.find_by_app_id_and_app_key(request.headers['HTTP_APP_ID'], request.headers['HTTP_APP_KEY'])
end

In felu asta nu avem nevoie de before_filter deasupra si nici de metoda aia de am facut-o acum

Expand Down
10 changes: 10 additions & 0 deletions app/controllers/api/v1/errors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ def index
end

def create
@subscriber = Subscriber.where("email = ? and website_id = ?", params['email'], @current_site.id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aici schimba @current_site cu current_site. Pentru ca facem schimbarea de mai sus.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

poti sa schimbi de fapt toata linia cu current_site.subscriber.find_by(email: params[:email])

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nu mai folosi @ decat daca vrei sa folosesti @subscriber in view adica in .html .json files. Altfel nu se justifica sa folosesti @.

@error = Issue.where("description = ?", params['message'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

asta nu prea are sens. description ala cel mai probabil o sa fie un backtrace sau un mesaj pe care il trimite siteu cu eroarea. Ti-am zis ca params['message'] asta nu prea are loc in momentul asta. Ce putem sa facem e sa adaugam un nou tabel cu mesajele. Si sa fie mesajele legate de issue_id si subscriber_id

if @subscriber.blank?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aici probabil ca trebuie sa schimbi doar cu if subscriber

@subscriber = Subscriber.create( email: params['email'], name: params['email'], created_at: Time.now, updated_at: Time.now, website_id: @current_site.id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wtf nu e nevoie sa setezi created_at si updated_at alea sunt salvate automat ..... Daca avem current_site foloseste
current_site.subscriber.create(...)

end

if @error.blank?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unless error probabil ..

@error = Issue.create(description: params['message'], created_at: Time.now, updated_at: Time.now, status: 1, website_id: @current_site.id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

description ca si mai sus nu isi are locul aici. ce trebuie sa salvezi e restul de informatii. foloseste current_site.issues.create(..) si nu mai seta website_id in create... Status nu e nevoie sa il setezi pentru ca are default.

SubscriberIssue.create(subscriber_id: @subscriber.id, issue_id: @error.id,created_at: Time.now, updated_at: Time.now)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sterge created_at si updated at din toate creaturile ca mor aici ...
image

end
end

def show
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
end
resources :subscribers, only: [:index]
resources :members, only: [:show]
resources :websites, only: [:index]
resources :websites, only: [:index, :create]
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddAppIdAndAppKeyToWebsites < ActiveRecord::Migration
def change
add_column :websites, :app_id, :integer
add_column :websites, :app_key, :string
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150605162303) do
ActiveRecord::Schema.define(version: 20150730064121) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -94,6 +94,8 @@
t.integer "member_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "app_id"
t.string "app_key"
end

add_index "websites", ["member_id"], name: "index_websites_on_member_id", using: :btree
Expand Down