shopify_app 5.0.2 → 6.0.0

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.
Files changed (110) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/.gitignore +4 -1
  5. data/.travis.yml +9 -0
  6. data/CHANGELOG +12 -1
  7. data/README.md +73 -108
  8. data/Rakefile +0 -1
  9. data/app/controllers/sessions_controller.rb +3 -0
  10. data/app/views/sessions/new.html.erb +17 -0
  11. data/config/routes.rb +10 -0
  12. data/lib/generators/shopify_app/controllers/controllers_generator.rb +29 -0
  13. data/lib/generators/shopify_app/install/install_generator.rb +83 -0
  14. data/lib/generators/shopify_app/install/templates/embedded_app.html.erb +22 -0
  15. data/lib/generators/shopify_app/install/templates/home_controller.rb +11 -0
  16. data/lib/generators/shopify_app/install/templates/index.html.erb +12 -0
  17. data/lib/generators/shopify_app/install/templates/omniauth.rb +2 -0
  18. data/lib/generators/shopify_app/install/templates/shopify_app.rb +6 -0
  19. data/lib/generators/shopify_app/install/templates/shopify_app_ready_script.html +8 -0
  20. data/lib/generators/shopify_app/install/templates/shopify_provider.rb +11 -0
  21. data/lib/generators/shopify_app/{templates/config/initializers → install/templates}/shopify_session_repository.rb +3 -3
  22. data/lib/generators/shopify_app/routes/routes_generator.rb +31 -0
  23. data/lib/generators/shopify_app/routes/templates/routes.rb +7 -0
  24. data/lib/generators/shopify_app/shop_model/shop_model_generator.rb +42 -0
  25. data/lib/generators/shopify_app/shop_model/templates/db/migrate/create_shops.rb +15 -0
  26. data/lib/generators/shopify_app/shop_model/templates/session_storage.rb +16 -0
  27. data/lib/generators/shopify_app/shop_model/templates/shop.rb +3 -0
  28. data/lib/generators/shopify_app/shop_model/templates/shopify_session_repository.rb +7 -0
  29. data/lib/generators/shopify_app/shopify_app_generator.rb +10 -63
  30. data/lib/generators/shopify_app/views/views_generator.rb +29 -0
  31. data/lib/shopify_app.rb +14 -13
  32. data/lib/shopify_app/configuration.rb +26 -36
  33. data/lib/shopify_app/controller.rb +18 -0
  34. data/lib/shopify_app/engine.rb +5 -0
  35. data/lib/shopify_app/login_protection.rb +34 -32
  36. data/lib/shopify_app/sessions_controller.rb +64 -0
  37. data/lib/shopify_app/shop.rb +15 -0
  38. data/lib/shopify_app/shopify_session_repository.rb +23 -21
  39. data/lib/shopify_app/version.rb +1 -1
  40. data/shopify_app.gemspec +5 -10
  41. data/test/app_templates/app/controllers/application_controller.rb +2 -0
  42. data/test/app_templates/config/application.rb +24 -0
  43. data/test/app_templates/config/initializers/shopify_app.rb +6 -0
  44. data/test/app_templates/config/routes.rb +3 -0
  45. data/test/controllers/sessions_controller_test.rb +82 -0
  46. data/test/dummy/app/controllers/application_controller.rb +3 -0
  47. data/test/dummy/app/controllers/home_controller.rb +7 -0
  48. data/test/dummy/config.ru +4 -0
  49. data/test/dummy/config/application.rb +26 -0
  50. data/test/dummy/config/boot.rb +5 -0
  51. data/test/dummy/config/database.yml +25 -0
  52. data/test/dummy/config/environment.rb +5 -0
  53. data/test/dummy/config/environments/test.rb +42 -0
  54. data/test/dummy/config/initializers/shopify_app.rb +6 -0
  55. data/test/dummy/config/routes.rb +10 -0
  56. data/test/dummy/config/secrets.yml +17 -0
  57. data/test/generators/install_generator_test.rb +103 -0
  58. data/test/generators/routes_generator_test.rb +33 -0
  59. data/test/generators/shop_model_generator_test.rb +40 -0
  60. data/test/generators/shopify_app_generator.rb +15 -0
  61. data/test/generators/views_generator_test.rb +15 -0
  62. data/test/shopify_app/configuration_test.rb +17 -0
  63. data/test/{lib/shopify_app → shopify_app}/in_memory_session_store_test.rb +6 -6
  64. data/test/{lib/shopify_app → shopify_app}/login_protection_test.rb +10 -7
  65. data/test/shopify_app/shopify_session_repository_test.rb +73 -0
  66. data/test/support/generator_test_helpers.rb +29 -0
  67. data/test/test_helper.rb +11 -3
  68. metadata +79 -61
  69. metadata.gz.sig +0 -0
  70. data/install.rb +0 -25
  71. data/lib/generators/shopify_app/README +0 -23
  72. data/lib/generators/shopify_app/USAGE +0 -22
  73. data/lib/generators/shopify_app/templates/app/assets/images/favicon.png +0 -0
  74. data/lib/generators/shopify_app/templates/app/assets/images/grid-18px.png +0 -0
  75. data/lib/generators/shopify_app/templates/app/assets/images/shopify-114.png +0 -0
  76. data/lib/generators/shopify_app/templates/app/assets/images/shopify-57.png +0 -0
  77. data/lib/generators/shopify_app/templates/app/assets/images/shopify-72.png +0 -0
  78. data/lib/generators/shopify_app/templates/app/assets/images/shopify.png +0 -0
  79. data/lib/generators/shopify_app/templates/app/assets/javascripts/application.js +0 -15
  80. data/lib/generators/shopify_app/templates/app/assets/stylesheets/application.css +0 -10
  81. data/lib/generators/shopify_app/templates/app/assets/stylesheets/imports.css.less +0 -5
  82. data/lib/generators/shopify_app/templates/app/assets/stylesheets/shopify_app.css.scss +0 -150
  83. data/lib/generators/shopify_app/templates/app/assets/stylesheets/shopify_app_buttons.css.less +0 -77
  84. data/lib/generators/shopify_app/templates/app/controllers/application_controller.rb +0 -13
  85. data/lib/generators/shopify_app/templates/app/controllers/home_controller.rb +0 -18
  86. data/lib/generators/shopify_app/templates/app/controllers/sessions_controller.rb +0 -52
  87. data/lib/generators/shopify_app/templates/app/helpers/home_helper.rb +0 -9
  88. data/lib/generators/shopify_app/templates/app/helpers/login_helper.rb +0 -3
  89. data/lib/generators/shopify_app/templates/app/helpers/tabs_helper.rb +0 -11
  90. data/lib/generators/shopify_app/templates/app/views/home/design.html.erb +0 -3542
  91. data/lib/generators/shopify_app/templates/app/views/home/index.html.erb +0 -168
  92. data/lib/generators/shopify_app/templates/app/views/home/welcome.html.erb +0 -110
  93. data/lib/generators/shopify_app/templates/app/views/layouts/application.html.erb +0 -79
  94. data/lib/generators/shopify_app/templates/app/views/sessions/new.html.erb +0 -21
  95. data/lib/generators/shopify_app/templates/config/initializers/omniauth.rb +0 -14
  96. data/lib/generators/shopify_app/templates/config/initializers/rails_4_shim.rb +0 -1
  97. data/lib/generators/shopify_app/templates/public/404.html +0 -40
  98. data/lib/generators/shopify_app/templates/public/422.html +0 -40
  99. data/lib/generators/shopify_app/templates/public/500.html +0 -40
  100. data/lib/generators/shopify_app/templates/public/favicon.png +0 -0
  101. data/lib/generators/shopify_app/templates/public/shopify-114.png +0 -0
  102. data/lib/generators/shopify_app/templates/public/shopify-57.png +0 -0
  103. data/lib/generators/shopify_app/templates/public/shopify-72.png +0 -0
  104. data/lib/shopify_app/railtie.rb +0 -17
  105. data/test/config/development_config_file.yml +0 -8
  106. data/test/config/empty_config_file.yml +0 -1
  107. data/test/config/other_config_file.yml +0 -4
  108. data/test/config/shopify_app.yml +0 -4
  109. data/test/lib/shopify_app/configuration_test.rb +0 -91
  110. data/test/lib/shopify_app/shopify_session_repository_test.rb +0 -70
@@ -0,0 +1,22 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <title>Shopify Embedded Example App</title>
5
+
6
+ <script src="//cdn.shopify.com/s/assets/external/app.js?<%= Time.now.strftime('%Y%m%d%H') %>"></script>
7
+ <script type="text/javascript">
8
+ ShopifyApp.init({
9
+ apiKey: "<%= ShopifyApp.configuration.api_key %>",
10
+ shopOrigin: '<%= "https://#{ @shop_session.url }" if @shop_session %>'
11
+ });
12
+ </script>
13
+
14
+ <%= stylesheet_link_tag 'application' %>
15
+ <%= javascript_include_tag 'application' %>
16
+ <%= csrf_meta_tags %>
17
+ </head>
18
+
19
+ <body>
20
+ <%= yield %>
21
+ </body>
22
+ </html>
@@ -0,0 +1,11 @@
1
+ class HomeController < ApplicationController
2
+ around_filter :shopify_session
3
+ <% if embedded_app? -%>
4
+ layout 'embedded_app'
5
+ <% end -%>
6
+
7
+ def index
8
+ @products = ShopifyAPI::Product.find(:all, :params => {:limit => 10})
9
+ end
10
+
11
+ end
@@ -0,0 +1,12 @@
1
+ <h3>Products</h3>
2
+ <table>
3
+ <tbody>
4
+ <% @products.each do |product| %>
5
+ <tr>
6
+ <td>
7
+ <a href='https://<%="#{@shop_session.url}/admin/products/#{product.id}"%>' target="_blank"> <%= product.id %> </a>
8
+ </td>
9
+ </tr>
10
+ <% end %>
11
+ </tbody>
12
+ </table>
@@ -0,0 +1,2 @@
1
+ Rails.application.config.middleware.use OmniAuth::Builder do
2
+ end
@@ -0,0 +1,6 @@
1
+ ShopifyApp.configure do |config|
2
+ config.api_key = "<%= opts[:api_key] || '<api_key>' %>"
3
+ config.secret = "<%= opts[:secret] || '<secret>' %>"
4
+ config.scope = "<%= opts[:scope] || 'read_orders, read_products' %>"
5
+ config.embedded_app = <%= opts[:embedded_app] || true %>
6
+ end
@@ -0,0 +1,8 @@
1
+ <script type="text/javascript">
2
+ ShopifyApp.ready(function(){
3
+ ShopifyApp.Bar.initialize({
4
+ title: "Home",
5
+ icon: "<%= asset_path('faveicon.png') %>"
6
+ });
7
+ });
8
+ </script>
@@ -0,0 +1,11 @@
1
+ provider :shopify,
2
+ ShopifyApp.configuration.api_key,
3
+ ShopifyApp.configuration.secret,
4
+
5
+ :scope => ShopifyApp.configuration.scope,
6
+
7
+ :setup => lambda {|env|
8
+ params = Rack::Utils.parse_query(env['QUERY_STRING'])
9
+ site_url = "https://#{params['shop']}"
10
+ env['omniauth.strategy'].options[:client_options][:site] = site_url
11
+ }
@@ -8,16 +8,16 @@
8
8
  #
9
9
  # class Shop < ActiveRecord::Base
10
10
  # def self.store(session)
11
- # shop = Shop.new(domain: session.url, token: session.token)
11
+ # shop = self.new(domain: session.url, token: session.token)
12
12
  # shop.save!
13
13
  # shop.id
14
14
  # end
15
15
  #
16
16
  # def self.retrieve(id)
17
- # if shop = Shop.where(id: id).first
17
+ # if shop = self.where(id: id).first
18
18
  # ShopifyAPI::Session.new(shop.domain, shop.token)
19
19
  # end
20
20
  # end
21
21
  # end
22
22
 
23
- ShopifySessionRepository.storage = InMemorySessionStore
23
+ ShopifyApp::SessionRepository.storage = InMemorySessionStore
@@ -0,0 +1,31 @@
1
+ require 'rails/generators/base'
2
+
3
+ module ShopifyApp
4
+ module Generators
5
+ class RoutesGenerator < Rails::Generators::Base
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ def inject_shopify_app_routes_into_application_routes
9
+ route(session_routes)
10
+ end
11
+
12
+ def disable_engine_routes
13
+ inject_into_file(
14
+ 'config/initializers/shopify_app.rb',
15
+ " config.routes = false\n",
16
+ before: 'end'
17
+ )
18
+ end
19
+
20
+ private
21
+
22
+ def session_routes
23
+ File.read(routes_file_path)
24
+ end
25
+
26
+ def routes_file_path
27
+ File.expand_path(find_in_source_paths('routes.rb'))
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,7 @@
1
+
2
+ controller :sessions do
3
+ get 'login' => :new, :as => :login
4
+ post 'login' => :create, :as => :authenticate
5
+ get 'auth/shopify/callback' => :callback
6
+ get 'logout' => :destroy, :as => :logout
7
+ end
@@ -0,0 +1,42 @@
1
+ require 'rails/generators/base'
2
+ require 'rails/generators/active_record'
3
+
4
+ module ShopifyApp
5
+ module Generators
6
+ class ShopModelGenerator < Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+ source_root File.expand_path('../templates', __FILE__)
9
+
10
+ def create_shop_model
11
+ copy_file 'shop.rb', 'app/models/shop.rb'
12
+ end
13
+
14
+ def create_shop_migration
15
+ copy_migration 'create_shops.rb'
16
+ end
17
+
18
+ def create_session_storage
19
+ copy_file 'session_storage.rb', 'app/models/session_storage.rb'
20
+ end
21
+
22
+ def create_session_storage_initializer
23
+ copy_file 'shopify_session_repository.rb', 'config/initializers/shopify_session_repository.rb', force: true
24
+ end
25
+
26
+ private
27
+
28
+ def copy_migration(migration_name, config = {})
29
+ migration_template(
30
+ "db/migrate/#{migration_name}",
31
+ "db/migrate/#{migration_name}",
32
+ config
33
+ )
34
+ end
35
+
36
+ # for generating a timestamp when using `create_migration`
37
+ def self.next_migration_number(dir)
38
+ ActiveRecord::Generators::Base.next_migration_number(dir)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,15 @@
1
+ class CreateShops < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :shops do |t|
4
+ t.string :shopify_domain, null: false
5
+ t.string :shopify_token, null: false
6
+ t.timestamps
7
+ end
8
+
9
+ add_index :shops, :shopify_domain, unique: true
10
+ end
11
+
12
+ def self.down
13
+ drop_table :shops
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ class SessionStorage
2
+ def self.store(session)
3
+ shop = self.find_or_initialize_by(shopify_domain: session.url)
4
+ shop.shopify_token = session.token
5
+ shop.save!
6
+ shop.id
7
+ end
8
+
9
+ def self.retrieve(id)
10
+ return unless id
11
+ shop = self.find(id)
12
+ ShopifyAPI::Session.new(shop.shopify_domain, shop.shopify_token)
13
+ rescue ActiveRecord::RecordNotFound
14
+ nil
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ class Shop < ActiveRecord::Base
2
+ include ShopifyApp::Shop
3
+ end
@@ -0,0 +1,7 @@
1
+ if Rails.configuration.cache_classes
2
+ ShopifyApp::SessionRepository.storage = SessionStorage
3
+ else
4
+ ActionDispatch::Reloader.to_prepare do
5
+ ShopifyApp::SessionRepository.storage = SessionStorage
6
+ end
7
+ end
@@ -1,69 +1,16 @@
1
- require 'rails/generators'
1
+ module ShopifyApp
2
+ module Generators
3
+ class ShopifyAppGenerator < Rails::Generators::Base
2
4
 
3
- class ShopifyAppGenerator < Rails::Generators::Base
4
- argument :api_key, :type => :string, :required => false
5
- argument :secret, :type => :string, :required => false
5
+ def run_all_generators
6
+ generate "shopify_app:install"
7
+ generate "shopify_app:shop_model"
6
8
 
7
- class_option :skip_routes, :type => :boolean, :default => false, :desc => 'pass true to skip route generation'
9
+ generate "shopify_app:controllers"
10
+ generate "shopify_app:views"
11
+ generate "shopify_app:routes"
12
+ end
8
13
 
9
- def self.source_root
10
- File.join(File.dirname(__FILE__), 'templates')
11
- end
12
-
13
- def copy_files
14
- directory 'app'
15
- directory 'public'
16
- directory 'config'
17
- end
18
-
19
- def remove_static_index
20
- remove_file 'public/index.html'
21
- end
22
-
23
- def add_config_variables
24
- return if api_key.blank? || secret.blank?
25
-
26
- inject_into_file 'config/application.rb', <<-DATA, :after => "class Application < Rails::Application\n"
27
-
28
- # Shopify API connection credentials:
29
- config.shopify.api_key = '#{api_key}'
30
- config.shopify.secret = '#{secret}'
31
- DATA
32
- end
33
-
34
- def add_bootstrap_gem
35
- gem_group :development, :test do
36
- gem "less-rails-bootstrap"
37
- gem 'therubyracer', :platforms => :ruby
38
14
  end
39
15
  end
40
-
41
- def add_routes
42
- unless options[:skip_routes]
43
- route_without_newline "root :to => 'home#index'"
44
- route "end"
45
- route_without_newline " delete 'logout' => :destroy"
46
- route_without_newline " get 'auth/shopify/callback' => :show"
47
- route_without_newline " post 'login' => :create"
48
- route_without_newline " get 'login' => :new"
49
- route_without_newline "controller :sessions do"
50
- route "get 'design' => 'home#design'"
51
- route_without_newline "get 'welcome' => 'home#welcome'"
52
- end
53
- end
54
-
55
- def display_readme
56
- Bundler.with_clean_env do
57
- run 'bundle install'
58
- end
59
-
60
- readme '../README'
61
- end
62
-
63
- private
64
-
65
- def route_without_newline(routing_code)
66
- sentinel = /\.routes\.draw do(?:\s*\|map\|)?\s*$/
67
- inject_into_file 'config/routes.rb', "\n #{routing_code}", { after: sentinel, verbose: false }
68
- end
69
16
  end
@@ -0,0 +1,29 @@
1
+ require 'rails/generators/base'
2
+
3
+ module ShopifyApp
4
+ module Generators
5
+ class ViewsGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("../../../../..", __FILE__)
7
+
8
+ def create_views
9
+ views.each do |view|
10
+ copy_file view
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def views
17
+ files_within_root('.', 'app/views/**/*.*')
18
+ end
19
+
20
+ def files_within_root(prefix, glob)
21
+ root = "#{self.class.source_root}/#{prefix}"
22
+
23
+ Dir["#{root}/#{glob}"].sort.map do |full_path|
24
+ full_path.sub(root, '.').gsub('/./', '/')
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,20 +1,21 @@
1
+ require 'shopify_app/version'
2
+
3
+ # deps
1
4
  require 'shopify_api'
2
5
  require 'omniauth-shopify-oauth2'
3
6
 
4
- module ShopifyApp
7
+ # config
8
+ require 'shopify_app/configuration'
9
+
10
+ # engine
11
+ require 'shopify_app/engine'
5
12
 
6
- def self.configuration
7
- @configuration ||= ShopifyApp::Configuration.new
8
- end
9
-
10
- def self.setup_session
11
- ShopifyAPI::Session.setup(:api_key => ShopifyApp.configuration.api_key, :secret => ShopifyApp.configuration.secret)
12
- end
13
- end
13
+ # helpers and concerns
14
+ require 'shopify_app/shop'
15
+ require 'shopify_app/controller'
16
+ require 'shopify_app/sessions_controller'
17
+ require 'shopify_app/login_protection'
14
18
 
19
+ # session repository
15
20
  require 'shopify_app/shopify_session_repository'
16
21
  require 'shopify_app/in_memory_session_store'
17
- require 'shopify_app/login_protection'
18
- require 'shopify_app/configuration'
19
- require 'shopify_app/railtie'
20
- require 'shopify_app/version'
@@ -1,46 +1,36 @@
1
- class ShopifyApp::Configuration
2
- VALID_KEYS = [:api_key, :secret, :myshopify_domain]
3
- attr_writer *VALID_KEYS
4
-
5
- def initialize(params={})
6
- self.params = default_params.merge(params)
7
- end
8
-
9
- VALID_KEYS.each do |meth|
10
- define_method meth do
11
- meth = meth.to_s
12
- config_from_env(meth) || config_from_rails(meth) || config_from_file(meth) || ''
1
+ module ShopifyApp
2
+ class Configuration
3
+
4
+ # Shopify App settings. These values should match the configuration
5
+ # for the app in your Shopify Partners page. Change your settings in
6
+ # `config/initializers/shopify_app.rb`
7
+ attr_accessor :api_key
8
+ attr_accessor :secret
9
+ attr_accessor :scope
10
+ attr_accessor :embedded_app
11
+ alias_method :embedded_app?, :embedded_app
12
+
13
+ # use the built in session routes?
14
+ attr_accessor :routes
15
+
16
+ def routes_enabled?
17
+ @routes
13
18
  end
14
- end
15
-
16
- private
17
-
18
- attr_accessor :params
19
19
 
20
- def config_from_env(meth)
21
- ENV["SHOPIFY_APP_#{meth.upcase}"]
22
- end
23
-
24
- def config_from_rails(meth)
25
- instance_variable_get("@#{meth}")
26
- end
27
-
28
- def config_from_file(meth)
29
- @config_file ||= load_config
30
- @config_file[Rails.env].try(:[], meth) || @config_file['common'].try(:[], meth)
20
+ def initialize
21
+ @routes = true
22
+ end
31
23
  end
32
24
 
33
- def load_config
34
- File.exist?(config_filepath) ? YAML.load_file(config_filepath) : {}
25
+ def self.configuration
26
+ @configuration ||= Configuration.new
35
27
  end
36
28
 
37
- def config_filepath
38
- params[:config_file]
29
+ def self.configuration=(config)
30
+ @configuration = config
39
31
  end
40
32
 
41
- def default_params
42
- {
43
- config_file: File.join(Rails.root, 'config', 'shopify_app.yml')
44
- }
33
+ def self.configure
34
+ yield configuration
45
35
  end
46
36
  end