shopify_app 5.0.2 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4074fe532a9c1606f958f24ce1c9ef8f07c58fb7
4
- data.tar.gz: 8d9832af0f7999c33225fb4d2e1c83c151bff70e
3
+ metadata.gz: 2180fbad771bc170d57ca9e9d84264db64adc578
4
+ data.tar.gz: 028708ad9fc92ce756fbf815468d586e2e0302ed
5
5
  SHA512:
6
- metadata.gz: 00c8fffe2c5f445fe0ef959b62156d032bae66260df5704fb2fdbc566f853475f5a4f66d848733cb1233ce3c82e8e5898a997da350014bbb08716544e1ffb50e
7
- data.tar.gz: a0178aeb4ac85acfd59d44d763a67de03ace96a0e426000b772db29f8f4c1e12ff3de68aac7fade16c715d335bddbdae298d7d8e76e91701ca1a8e7350d86268
6
+ metadata.gz: d07a8720342f16795447fd929d4fb9d5933ba7cbbe27b085f8c00663548ef3ff5bd5ef826ec5062e30e00d476ac2bea7818e1eefa61b4e06f0831027f4ce309a
7
+ data.tar.gz: 5c17a5290b05876ecd0bc321dc9477612d7f1d8c87689ff506d6e5ac21f0df4b2c936b1e9d28945d7d2fea0c25eacbf93fc129555fe7f47cc63400e4f80db87c
Binary file
data.tar.gz.sig CHANGED
Binary file
data/.gitignore CHANGED
@@ -4,4 +4,7 @@ Gemfile.lock
4
4
  pkg/*
5
5
  .DS_Store
6
6
  .yardoc
7
- doc/
7
+ doc/
8
+ *.log
9
+ *.sqlite3
10
+ test/tmp/*
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.0.0
5
+
6
+ gemfile:
7
+ - Gemfile
8
+
9
+ script: "bundle exec rake test"
data/CHANGELOG CHANGED
@@ -1,5 +1,16 @@
1
+ 6.0.0
2
+ -----
3
+ * Re-written as a proper rails engine
4
+ * New modular generators
5
+ * generates embedded apps by default
6
+ * can bootstrap your app with a standard shop table and model
7
+ * code now lives as concerns in the engine which are included in your controllers
8
+ feel free to override anything you need in your app
9
+
10
+ Historical
11
+ ----------
1
12
  * re-styled with Twitter Bootstrap css framework and updated docs [warren]
2
13
  * Require shopify_api gem via proper railtie setup [willem]
3
14
  * Don't require shopify.yml when using environment variables [cody]
4
15
  * Renamed instances of current_shop to shop_session to maintain logic
5
- * Replace old LoginController with a RESTful SessionsController
16
+ * Replace old LoginController with a RESTful SessionsController
data/README.md CHANGED
@@ -1,18 +1,30 @@
1
- # Shopify App
1
+ Shopify App [![Build Status](https://travis-ci.org/Shopify/shopify_app.png)](https://travis-ci.org/Shopify/shopify_app)
2
+ ===========
2
3
 
3
- [![Build Status](https://travis-ci.org/Shopify/shopify_app.png)](https://travis-ci.org/Shopify/shopify_app)
4
+ Shopify Application Rails engine and generator
4
5
 
5
- Shopify application generator for Rails 3.1 and Rails 4.0
6
6
 
7
- ## Description
7
+ Description
8
+ -----------
9
+ This gem includes some common code and generators for writing Rails applications using the Shopify API.
8
10
 
9
- This gem makes it easy to get a Rails 3.1 or Rails 4.0 app up and running with the Shopify API.
11
+ *Note: It's recommended to use this on a new Rails project, so that the generator won't overwrite/delete some of your files.*
10
12
 
11
- The generator creates a basic SessionsController for authenticating with your shop and a HomeController which displays basic information about your products, orders and articles.
12
13
 
13
- *Note: It's recommended to use this on a new Rails project, so that the generator won't overwrite/delete some of your files.*
14
+ Becoming a Shopify App Developer
15
+ --------------------------------
16
+ If you don't have a Shopify Partner account yet head over to http://shopify.com/partners to create one, you'll need it before you can start developing apps.
17
+
18
+ Once you have a Partner account create a new application to get an Api key and other Api credentials. To create a development application set the Application Callback URL to
19
+
20
+ http://localhost:3000/login
21
+
22
+ This way you'll be able to run the app on your local machine.
23
+
14
24
 
15
- ## Installation
25
+ Installation
26
+ ------------
27
+ To get started add shopify_app to your Gemfile and bundle install
16
28
 
17
29
  ``` sh
18
30
  # Create a new rails app
@@ -24,59 +36,72 @@ $ echo "gem 'shopify_app'" >> Gemfile
24
36
  $ bundle install
25
37
  ```
26
38
 
27
- ## Usage
39
+ Now we are ready to run any of the shopify_app generators. The following section explains the generators and what they can do.
28
40
 
29
- ``` sh
30
- $ rails generate shopify_app your_app_api_key your_app_secret
41
+
42
+ Generators
43
+ ----------
44
+
45
+ ### Install Generator
46
+
47
+ ```sh
48
+ $ rails generate shopify_app:install
49
+
50
+ # or optionally with arguments:
51
+
52
+ $ rails generate shopify_app:install -api_key=<your_api_key> -secret=<your_app_secret>
31
53
  ```
32
54
 
33
- If you don't have an API key yet, create a Shopify Partner account at http://shopify.com/partners and create an app. You can also create test shops once you're logged in as a partner.
55
+ Other options include:
56
+ * `scope` - the Oauth access scope required for your app, eg 'read_products, write_orders'. For more information read the [docs](http://docs.shopify.com/api/tutorials/oauth)
57
+ * `embedded` - the default is to generate an [embedded app](http://docs.shopify.com/embedded-app-sdk), if you want a legacy non-embedded app then set this to false, `-embedded false`
34
58
 
35
- When you create your app in the Shopify Partner Account, set the Application Callback URL to
59
+ You can update any of these settings later on easily, the arguments are simply for convenience.
36
60
 
37
- http://localhost:3000/login
61
+ The generator creates a basic SessionsController for authenticating with your shop and a HomeController which displays basic information about your products using the ShopifyAPI. The generated controllers include concerns provided by this gem - in this way code sharing is possible and if some of these core methods are updated everyone can benefit. It is completely safe to override any of the methods provided by this gem in your application.
38
62
 
39
- You can also create a private application that only works for your shop by visiting https://YOUR-SHOP.myshopify.com/admin/apps/private.
63
+ After running the `install` generator you can start your app with `bundle exec rails server` and install your app by visiting localhost.
40
64
 
41
- ### Example
42
65
 
43
- ``` sh
44
- $ rails generate shopify_app edffbb1bb793e2750686e6f4647a384a fed5bb18hde3e2750686e6f4647a781a
66
+ ### Shop Model Generator
67
+
68
+ ```sh
69
+ $ rails generate shopify_app:shop_model
45
70
  ```
46
71
 
47
- This will create a LoginController and a HomeController with their own views.
72
+ The install generator doesn't create any database models for you and if you are starting a new app its quite likely that you will want one (most our internally developed apps do!). This generator creates a simple shop model and a migration. It also create a model called `SessionStorage` which interacts with `ShopifyApp::SessionRepository`. Check out the later section to learn more about `ShopifyApp::SessionRepository`
48
73
 
49
- ## Configuring Shopify App
74
+ *Note that you will need to run rake db:migrate after this generator*
50
75
 
51
- It's possible to set your API key and secret key from different sources:
76
+ ### Controllers, Routes and Views
52
77
 
53
- * `SHOPIFY_APP_API_KEY` and `SHOPIFY_APP_SECRET` environment variables
54
- * Configuration in a Rails `<environment>.rb` config file
78
+ The last group of generators are for your convenience when you want to start overriding code included as part of the Rails engine. For example by default the engine provides a simple SessionController, if you run the `rails generate shopify_app:controllers` generator then this code gets copied out into your app so you can start adding to it. Routes and views follow the exact same pattern.
55
79
 
56
- ``` ruby
57
- config.shopify.api_key = 'your api key'
58
- config.shopify.secret = 'your secret'
59
- ```
60
80
 
61
- * Configuration loaded from `<environment>` key in `shopify_app.yml`
81
+ ### Default Generator
82
+
83
+ If you just run `rails generate shopify_app` then all the generators will be run for you. This is how we do it internally!
84
+
62
85
 
63
- ``` yaml
64
- development:
65
- api_key: your api key
66
- secret: your secret
67
- ```
68
86
 
69
- * Configuration loaded from common key in `shopify_app.yml`
87
+ Managing Api Keys
88
+ -----------------
70
89
 
71
- ``` yaml
72
- common:
73
- api_key: your api key
74
- secret: your secret
90
+ The `install` generator places your Api credentials directly into the shopify_app initializer which is convient and fine for development but once your app goes into production **your api credentials should not be in source control**. When we develop apps we keep our keys in environment variables so a production shopify_app initializer would look like this:
91
+
92
+ ```ruby
93
+ ShopifyApp.configure do |config|
94
+ config.api_key = ENV['SHOPIFY_CLIENT_API_KEY']
95
+ config.secret = ENV['SHOPIFY_CLIENT_API_SECRET']
96
+ config.scope = 'read_customers, read_orders, write_products'
97
+ config.embedded_app = true
98
+ end
75
99
  ```
76
100
 
77
- ## Set up your ShopifySessionRepository.store
101
+ ShopifyApp::SessionRepository
102
+ -----------------------------
78
103
 
79
- `ShopifySessionRepository` allows you as a developer to define how your sessions are retrieved and
104
+ `ShopifyApp::SessionRepository` allows you as a developer to define how your sessions are retrieved and
80
105
  stored for a shop. This can simply be your `Shop` model that stores the API Token and shop name. If
81
106
  you are using ActiveRecord, then all you need to implement is `self.store(shopify_session)` and
82
107
  `self.retrieve(id)` in order to store the record on disk or retrieve it for use at a later point.
@@ -88,98 +113,38 @@ Your ActiveRecord model would look something like this:
88
113
  ```ruby
89
114
  class Shop < ActiveRecord::Base
90
115
  def self.store(session)
91
- shop = Shop.new(domain: session.url, token: session.token)
116
+ shop = self.new(domain: session.url, token: session.token)
92
117
  shop.save!
93
118
  shop.id
94
119
  end
95
120
 
96
121
  def self.retrieve(id)
97
- if shop = Shop.where(id: id).first
122
+ if shop = self.where(id: id).first
98
123
  ShopifyAPI::Session.new(shop.domain, shop.token)
99
124
  end
100
125
  end
101
126
  end
102
127
  ```
103
128
 
104
- By default you will have an in memory store but it really won't work on multi-server environments since
129
+ By default you will have an in memory store but it **won't work** on multi-server environments since
105
130
  they won't be sharing the static data that would be required in case your user gets directed to a
106
131
  different server by your load balancer.
107
132
 
108
133
  The in memory store also does not behave well on Heroku because the session data would be destroyed
109
134
  when a dyno is killed due to inactivity.
110
135
 
111
- Changing the `ShopifySessionRepository.storage` can simply be done by editing
136
+ Changing the `ShopifyApp::SessionRepository.storage` can simply be done by editing
112
137
  `config/initializers/shopify_session_repository.rb` to use the correct model.
113
138
 
114
139
  ```ruby
115
- ShopifySessionRepository.storage = 'Shop'
116
- ```
117
-
118
- ## Set your required API permissions
119
-
120
- Before making API requests, your application must state which API permissions it requires from the shop it's installed in. These requested permissions will be listed on the screen the merchant sees when approving your app to be installed in their shop.
121
-
122
- Start by reviewing the documentation on API permission scopes: http://docs.shopify.com/api/tutorials/oauth
123
-
124
- When you know which ones your app will need, add a scope line to the `config/initializers/omniauth.rb` file.
125
-
126
- ### Example
127
-
128
- Make this change to request write access to products and read access to orders:
129
-
130
- ``` ruby
131
- Rails.application.config.middleware.use OmniAuth::Builder do
132
- provider :shopify,
133
- ShopifyApp.configuration.api_key,
134
- ShopifyApp.configuration.secret,
135
- :scope => "write_products,read_orders",
136
- :setup => lambda {|env|
137
- params = Rack::Utils.parse_query(env['QUERY_STRING'])
138
- site_url = "https://#{params['shop']}"
139
- env['omniauth.strategy'].options[:client_options][:site] = site_url
140
- }
141
- end
142
- ```
143
-
144
- *Note that you can change your API permission scopes on the fly, but the merchant will have to approve each change and your computed API password will change.*
145
-
146
- ## After running the generator
147
-
148
- First, start your application:
149
-
150
- ``` sh
151
- $ rails server
152
- ```
153
-
154
- Now visit http://localhost:3000 and install your application in a Shopify store. Even if Rails tells you to visit your app at http://0.0.0.0:3000, go to http://localhost:3000.
155
-
156
- After your application has been given whatever API permissions you requested by the shop, you're ready to start experimenting with the Shopify API.
157
-
158
- ## Rails 3.0 (as in before 3.1) Support
159
-
160
- Rails 3.0 (as in before the big changes in 3.1) is supported on a branch of our github repo: https://github.com/Shopify/shopify_app/tree/rails_3.0_support
161
-
162
- ## Common problems
163
-
164
- If you are getting the following error:
165
-
166
- ```
167
- Faraday::Error::ConnectionFailed error when accessing app.
168
- ```
169
-
170
- It probably means that the CA certificate on your computer is out of date. A simple solution on the Mac is to install XCode.
171
-
172
- If you are getting the following error:
173
-
174
- ```
175
- ActiveResource::ForbiddenAccess in HomeController#index
176
- Failed. Response code = 403. Response message = Forbidden.
140
+ ShopifyApp::SessionRepository.storage = 'Shop'
177
141
  ```
178
142
 
179
- It means that you have not set appropriate permissions in your `config/initializers/omniauth.rb` file for what you are trying to do in your HomeController#index action. Example: you set your permissions to 'write_content' because that's what your app will do, but your HomeController#index still has that default code generated by shopify_app which attempts to read products and orders, neither being covered by the 'write_content' scope.
143
+ If you run the `shop_model` generator it will create the required code to use the generated Shop model as the SessionRepository and update the initializer.
180
144
 
181
- ## Questions or problems?
182
145
 
146
+ Questions or problems?
147
+ ----------------------
183
148
  http://api.shopify.com <= Read up on the possible API calls!
184
149
 
185
150
  http://ecommerce.shopify.com/c/shopify-apis-and-technology <= Ask questions!
data/Rakefile CHANGED
@@ -2,7 +2,6 @@ require 'bundler/gem_tasks'
2
2
  require 'rake/testtask'
3
3
 
4
4
  Rake::TestTask.new do |t|
5
- t.libs << 'lib/shopify_app'
6
5
  t.libs << 'test'
7
6
  t.test_files = FileList['test/**/*_test.rb']
8
7
  t.verbose = true
@@ -0,0 +1,3 @@
1
+ class SessionsController < ApplicationController
2
+ include ShopifyApp::SessionsController
3
+ end
@@ -0,0 +1,17 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Install Page</title>
5
+ </head>
6
+ <body>
7
+
8
+ <h2>Install the application</h2>
9
+
10
+ <form method="GET" action="login">
11
+ <label>Type shop name:</label>
12
+ <input type="text" name="shop" placeholder="blabla.myshopify.com"/>
13
+ <button type='submit'>Install</buttom>
14
+ </form>
15
+
16
+ </body>
17
+ </html>
@@ -0,0 +1,10 @@
1
+ if ShopifyApp.configuration.routes_enabled?
2
+ ShopifyApp::Engine.routes.draw do
3
+ controller :sessions do
4
+ get 'login' => :new, :as => :login
5
+ post 'login' => :create, :as => :authenticate
6
+ get 'auth/shopify/callback' => :callback
7
+ get 'logout' => :destroy, :as => :logout
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,29 @@
1
+ require 'rails/generators/base'
2
+
3
+ module ShopifyApp
4
+ module Generators
5
+ class ControllersGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("../../../../..", __FILE__)
7
+
8
+ def create_controllers
9
+ controllers.each do |controller|
10
+ copy_file controller
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def controllers
17
+ files_within_root('.', 'app/controllers/*.*')
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
@@ -0,0 +1,83 @@
1
+ require 'rails/generators/base'
2
+ require 'rails/generators/active_record'
3
+
4
+ module ShopifyApp
5
+ module Generators
6
+ class InstallGenerator < Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+ source_root File.expand_path('../templates', __FILE__)
9
+ attr_reader :opts
10
+
11
+ def initialize(args, *options)
12
+ @opts = Hash[options.first.join(' ').scan(/--?([^=\s]+)(?:=(\S+))?/)]
13
+ super(args, *options)
14
+ end
15
+
16
+ def create_shopify_app_initializer
17
+ template 'shopify_app.rb', 'config/initializers/shopify_app.rb'
18
+ end
19
+
20
+ def create_and_inject_into_omniauth_initializer
21
+ unless File.exist? "config/initializers/omniauth.rb"
22
+ copy_file 'omniauth.rb', 'config/initializers/omniauth.rb'
23
+ end
24
+
25
+ inject_into_file(
26
+ 'config/initializers/omniauth.rb',
27
+ File.read(File.expand_path(find_in_source_paths('shopify_provider.rb'))),
28
+ after: "Rails.application.config.middleware.use OmniAuth::Builder do\n"
29
+ )
30
+ end
31
+
32
+ def create_shopify_session_repository_initializer
33
+ copy_file 'shopify_session_repository.rb', 'config/initializers/shopify_session_repository.rb'
34
+ end
35
+
36
+ def inject_embedded_app_options_to_application
37
+ if embedded_app?
38
+ application "config.action_dispatch.default_headers.delete('X-Frame-Options')"
39
+ application "config.action_dispatch.default_headers['P3P'] = 'CP=\"Not used\"'"
40
+ end
41
+ end
42
+
43
+ def inject_into_application_controller
44
+ inject_into_class(
45
+ "app/controllers/application_controller.rb",
46
+ 'ApplicationController',
47
+ " include ShopifyApp::Controller\n"
48
+ )
49
+ end
50
+
51
+ def create_embedded_app_layout
52
+ if embedded_app?
53
+ copy_file 'embedded_app.html.erb', 'app/views/layouts/embedded_app.html.erb'
54
+ end
55
+ end
56
+
57
+ def create_home_controller
58
+ template 'home_controller.rb', 'app/controllers/home_controller.rb'
59
+ end
60
+
61
+ def create_home_index_view
62
+ copy_file 'index.html.erb', 'app/views/home/index.html.erb'
63
+ if embedded_app?
64
+ prepend_to_file(
65
+ 'app/views/home/index.html.erb',
66
+ File.read(File.expand_path(find_in_source_paths('shopify_app_ready_script.html')))
67
+ )
68
+ end
69
+ end
70
+
71
+ def add_home_index_route
72
+ route "root :to => 'home#index'"
73
+ end
74
+
75
+ private
76
+
77
+ def embedded_app?
78
+ opts[:embedded] != 'false'
79
+ end
80
+
81
+ end
82
+ end
83
+ end