shopqi-app 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -11,7 +11,7 @@
11
11
 
12
12
  $ rails g shopqi_app client_id client_secret
13
13
 
14
- `client_id`, `client_secret` 在注册 Application 后显示,注册时 `REDIRECT URI` 填写 localhost:3000/app/callback
14
+ `client_id`, `client_secret` 在注册 Application 后显示,注册时 `REDIRECT URI` 填写 localhost:3000/callback
15
15
 
16
16
  ## 配置
17
17
 
@@ -22,7 +22,7 @@
22
22
  client_id: 'dffd069fa92096022628a1f7eb174bc85d90278b8c90ec7ebadfbd94924bd2b8'
23
23
  secret: 'e1805141c81c496e4779371f41263c9f74ac2ffd3728302296b8af10b64d9c9b'
24
24
  scope: 'read_products read_orders'
25
- redirect_uri: 'http://localhost:3000/app/callback'
25
+ redirect_uri: 'http://localhost:3000/callback'
26
26
 
27
27
  ## 最后
28
28
 
@@ -0,0 +1,25 @@
1
+ module ShopQiApp
2
+ class OmniauthCallbacksController < ApplicationController
3
+ skip_before_filter :authenticate_shop!, only: :shopqi
4
+ before_filter :assert_omniauth_auth
5
+
6
+ def shopqi
7
+ shop = Shop.find_for_shopqi_oauth(request.env["omniauth.auth"])
8
+ sign_in_and_redirect shop
9
+ end
10
+
11
+ protected
12
+ def assert_omniauth_auth
13
+ unless request.env['omniauth.auth']
14
+ flash[:error] = "Could not log in to store."
15
+ redirect_to main_app.login_path
16
+ end
17
+ end
18
+
19
+ def sign_in_and_redirect(shop)
20
+ cookies.permanent.signed[:shop_id] = shop.id
21
+ redirect_to main_app.root_path
22
+ end
23
+
24
+ end
25
+ end
@@ -1,6 +1,6 @@
1
1
  module ShopQiApp
2
2
  class SessionsController < ApplicationController
3
- skip_before_filter :authenticate_shop!, only: [:new, :create, :shopqi_login]
3
+ skip_before_filter :authenticate_shop!, only: [:new, :shopqi_login]
4
4
 
5
5
  def new
6
6
  if params[:shop].present?
@@ -8,32 +8,17 @@ module ShopQiApp
8
8
  end
9
9
  end
10
10
 
11
- def create
12
- if data = request.env['omniauth.auth']
13
- session[:shopqi] = {
14
- url: params[:shop],
15
- access_token: data['credentials']['token'],
16
- shop: data['extra']['raw_info']['shop']
17
- }
18
- redirect_to main_app.root_path
19
- else
20
- flash[:error] = "Could not log in to store."
21
- redirect_to login_path
22
- end
23
- end
24
-
25
11
  def shopqi_login
26
12
  if signed_in?
27
13
  redirect_to main_app.root_path
28
14
  else
29
- redirect_to login_path(shop: params[:shop])
15
+ redirect_to main_app.login_path(shop: params[:shop])
30
16
  end
31
17
  end
32
18
 
33
19
  def destroy
34
- session[:shopqi] = nil
20
+ cookies.delete(:shop_id)
35
21
  redirect_to main_app.root_path
36
22
  end
37
-
38
23
  end
39
24
  end
@@ -5,15 +5,15 @@ module ShopQiApp
5
5
  end
6
6
 
7
7
  def signed_in?
8
- session[:shopqi]
8
+ !cookies.signed[:shop_id].blank?
9
9
  end
10
10
 
11
11
  def current_shop
12
- session[:shopqi] && session[:shopqi][:shop]
12
+ @current_shop ||= (signed_in? && Shop.find(cookies.signed[:shop_id]))
13
13
  end
14
14
 
15
15
  def shop_url
16
- "http://#{current_shop['shopqi_domain']}"
16
+ "http://#{current_shop.shopqi_domain}"
17
17
  end
18
18
  end
19
19
  end
@@ -3,6 +3,6 @@
3
3
  %h1
4
4
  安装应用
5
5
  %small 请输入商店 URL 地址
6
- = form_tag login_path, :class => 'form-search' do
6
+ = form_tag main_app.login_path, :class => 'form-search' do
7
7
  = text_field_tag 'shop', nil, :placeholder => '商店 URL 地址', :class => 'btn-large', :id => 'shop'
8
8
  = submit_tag '安装', :class => 'btn btn-primary btn-large'
@@ -18,3 +18,4 @@ Example:
18
18
  app/views/home/
19
19
  app/views/layouts/application.html.haml
20
20
  config/app_secret_config.yml
21
+ db/migrate/create_shops.rb
@@ -1,4 +1,7 @@
1
+ require 'rails/generators/active_record'
2
+
1
3
  class ShopQiAppGenerator < Rails::Generators::Base
4
+ include Rails::Generators::Migration
2
5
  namespace 'shopqi_app'
3
6
  source_root File.expand_path('../templates', __FILE__)
4
7
 
@@ -10,6 +13,10 @@ class ShopQiAppGenerator < Rails::Generators::Base
10
13
  template 'config/app_secret_config.yml.erb', 'config/app_secret_config.yml'
11
14
  end
12
15
 
16
+ def install_migration
17
+ migration_template 'db/migrate/create_shops.rb', 'db/migrate/create_shops.rb'
18
+ end
19
+
13
20
  def remove_files
14
21
  remove_file 'public/index.html'
15
22
  remove_file 'app/assets/javascripts/application.js'
@@ -18,11 +25,15 @@ class ShopQiAppGenerator < Rails::Generators::Base
18
25
  end
19
26
 
20
27
  def add_routes
21
- route "mount ShopQiApp::Engine => '/app'"
28
+ route "use_shopqi"
22
29
  route "root :to => 'home#index'"
23
30
  end
24
31
 
25
32
  def show
26
33
  readme "README"
27
34
  end
35
+
36
+ def self.next_migration_number(dirname)
37
+ ActiveRecord::Generators::Base.next_migration_number(dirname)
38
+ end
28
39
  end
@@ -2,10 +2,6 @@
2
2
 
3
3
  There is something that you need to do before you use shopqi-app.
4
4
 
5
- Step 0.
6
- If you DO NOT use database, you MUST comment the line below in config/application.rb
7
- # require "active_record/railtie"
8
-
9
5
  Step 1.
10
6
  UNCOMMENT the line below in Gemfile
11
7
  gem 'therubyracer', :platforms => :ruby
@@ -7,7 +7,7 @@ class HomeController < ApplicationController
7
7
  @orders = Shopkit.orders per_page: 3
8
8
  @products = Shopkit.products per_page: 5
9
9
  else
10
- redirect_to shopqi_app.login_path
10
+ redirect_to login_path
11
11
  end
12
12
  end
13
13
  end
@@ -0,0 +1,9 @@
1
+ class Shop < ActiveRecord::Base
2
+ attr_accessible :shop_id, :name, :shopqi_domain, :access_token
3
+
4
+ def self.find_for_shopqi_oauth(data)
5
+ shop = data.extra.raw_info.shop
6
+ where(shop_id: shop.id).first_or_create! name: shop.name, shopqi_domain: shop.shopqi_domain, access_token: data.credentials.token
7
+ end
8
+
9
+ end
@@ -27,7 +27,7 @@
27
27
  商店后台
28
28
  %li.divider
29
29
  %li
30
- =link_to shopqi_app.sign_out_path do
30
+ =link_to sign_out_path do
31
31
  %i.icon-off
32
32
  退出
33
33
 
@@ -3,7 +3,7 @@ defaults: &defaults
3
3
  client_id: <%= client_id %>
4
4
  secret: <%= client_secret %>
5
5
  scope: 'read_products read_orders'
6
- redirect_uri: 'http://localhost:3000/app/callback'
6
+ callback_path: '/callback'
7
7
 
8
8
  development:
9
9
  <<: *defaults
@@ -17,4 +17,4 @@ production:
17
17
  client_id: <%= client_id %>
18
18
  client_secret: <%= client_secret %>
19
19
  scope: 'read_products read_orders'
20
- redirect_uri: 'http://localhost:3000/app/callback'
20
+ callback_path: '/callback'
@@ -0,0 +1,11 @@
1
+ class CreateShops < ActiveRecord::Migration
2
+ def change
3
+ create_table :shops do |t|
4
+ t.integer :shop_id , unique: true, null: false
5
+ t.string :name , limit: 32 , null: false
6
+ t.string :shopqi_domain, limit: 64 , null: false
7
+ t.string :access_token , limit: 64
8
+ end
9
+ add_index :shops, :shop_id, :unique => true
10
+ end
11
+ end
data/lib/shopqi-app.rb CHANGED
@@ -8,4 +8,8 @@ require 'shopkit'
8
8
 
9
9
  module ShopQiApp
10
10
  autoload :Helpers, "shopqi_app/helpers"
11
+
12
+ module Rails
13
+ autoload :Routes, "shopqi_app/rails/routes"
14
+ end
11
15
  end
@@ -9,6 +9,10 @@ module ShopQiApp
9
9
  g.integration_tool :rspec
10
10
  end
11
11
 
12
+ initializer "shopqi_app.routes" do
13
+ ShopQiApp::Rails::Routes.install
14
+ end
15
+
12
16
  initializer "shopqi_app.acronym" do
13
17
  ActiveSupport::Inflector.inflections do |inflect|
14
18
  inflect.acronym 'ShopQi'
@@ -20,6 +24,23 @@ module ShopQiApp
20
24
  include ShopQiApp::Helpers
21
25
  end
22
26
  end
23
- end
24
27
 
28
+ initializer "shopqi_app.omniauth" do |app|
29
+ app.middleware.use OmniAuth::Builder do
30
+ provider :shopqi,
31
+ (SecretSetting.oauth.client_id rescue nil), # 调用 generator 时还没有 app_secret_config.yml 文件
32
+ (SecretSetting.oauth.secret rescue nil),
33
+ :scope => (SecretSetting.oauth.scope rescue nil),
34
+ :callback_path => (SecretSetting.oauth.callback_path rescue nil),
35
+ :setup => lambda {|env|
36
+ params = Rack::Utils.parse_query(env['QUERY_STRING'])
37
+ #site_url = "https://#{params['shop']}"
38
+ site_url = "http://#{params['shop']}"
39
+ env['omniauth.strategy'].options[:client_options][:site] = site_url
40
+ }
41
+ end
42
+
43
+ OmniAuth.config.on_failure{|env| raise env['omniauth.error']}
44
+ end
45
+ end
25
46
  end
@@ -0,0 +1,19 @@
1
+ module ShopQiApp
2
+ module Rails
3
+ class Routes
4
+ module Helper
5
+ def use_shopqi(options = {}, &block)
6
+ options.reverse_merge! callbacks: 'shopqi_app/omniauth_callbacks'
7
+ self.match 'login' => "shopqi_app/sessions#new"
8
+ self.get 'callback_login' => "shopqi_app/sessions#shopqi_login" # 从 ShopQi 后台管理点击应用直接登录
9
+ self.get 'callback' => "#{options[:callbacks]}#shopqi"
10
+ self.get 'sign_out' => "shopqi_app/sessions#destroy"
11
+ end
12
+ end
13
+
14
+ def self.install
15
+ ActionDispatch::Routing::Mapper.send :include, ShopQiApp::Rails::Routes::Helper
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module ShopQiApp
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shopqi-app
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-05 00:00:00.000000000 Z
12
+ date: 2012-07-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -148,14 +148,15 @@ extra_rdoc_files: []
148
148
  files:
149
149
  - app/helpers/shopqi_app/application_helper.rb
150
150
  - app/controllers/shopqi_app/application_controller.rb
151
+ - app/controllers/shopqi_app/omniauth_callbacks_controller.rb
151
152
  - app/controllers/shopqi_app/sessions_controller.rb
152
153
  - app/models/secret_setting.rb
153
154
  - app/views/shopqi_app/sessions/new.html.haml
154
- - config/initializers/omniauth.rb
155
- - config/routes.rb
155
+ - lib/generators/shopqi_app/templates/db/migrate/create_shops.rb
156
156
  - lib/generators/shopqi_app/templates/app/helpers/home_helper.rb
157
157
  - lib/generators/shopqi_app/templates/app/controllers/application_controller.rb
158
158
  - lib/generators/shopqi_app/templates/app/controllers/home_controller.rb
159
+ - lib/generators/shopqi_app/templates/app/models/shop.rb
159
160
  - lib/generators/shopqi_app/templates/app/assets/javascripts/application.js.coffee
160
161
  - lib/generators/shopqi_app/templates/app/assets/stylesheets/application.css.less.erb
161
162
  - lib/generators/shopqi_app/templates/app/views/layouts/application.html.haml
@@ -166,6 +167,7 @@ files:
166
167
  - lib/generators/shopqi_app/shopqi_app_generator.rb
167
168
  - lib/shopqi-app.rb
168
169
  - lib/shopqi_app/version.rb
170
+ - lib/shopqi_app/rails/routes.rb
169
171
  - lib/shopqi_app/engine.rb
170
172
  - lib/shopqi_app/helpers.rb
171
173
  - lib/tasks/shopqi_app_tasks.rake
@@ -186,7 +188,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
186
188
  version: '0'
187
189
  segments:
188
190
  - 0
189
- hash: 62860785
191
+ hash: 557789341
190
192
  required_rubygems_version: !ruby/object:Gem::Requirement
191
193
  none: false
192
194
  requirements:
@@ -195,7 +197,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
195
197
  version: '0'
196
198
  segments:
197
199
  - 0
198
- hash: 62860785
200
+ hash: 557789341
199
201
  requirements: []
200
202
  rubyforge_project:
201
203
  rubygems_version: 1.8.24
@@ -1,16 +0,0 @@
1
- Rails.application.config.middleware.use OmniAuth::Builder do
2
- provider :shopqi,
3
- (SecretSetting.oauth.client_id rescue nil), # 调用 generator 时还没有 app_secret_config.yml 文件
4
- (SecretSetting.oauth.secret rescue nil),
5
- :scope => (SecretSetting.oauth.scope rescue nil),
6
- :callback_path => '/app/callback',
7
- :setup => lambda {|env|
8
- params = Rack::Utils.parse_query(env['QUERY_STRING'])
9
- #site_url = "https://#{params['shop']}"
10
- site_url = "http://#{params['shop']}"
11
- Rails.logger.info(env['omniauth.strategy'])
12
- env['omniauth.strategy'].options[:client_options][:site] = site_url
13
- }
14
- end
15
-
16
- OmniAuth.config.on_failure{|env| raise env['omniauth.error']}
data/config/routes.rb DELETED
@@ -1,6 +0,0 @@
1
- ShopQiApp::Engine.routes.draw do
2
- match 'login' => 'sessions#new'
3
- get 'callback_login' => 'sessions#shopqi_login' # 从 ShopQi 后台管理点击应用直接登录
4
- get 'callback' => 'sessions#create'
5
- get 'sign_out' => 'sessions#destroy'
6
- end