Skip to content

Commit

Permalink
generate shopify app
Browse files Browse the repository at this point in the history
  • Loading branch information
mejiaro committed Jul 14, 2017
1 parent 241faf7 commit 200537e
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ gem 'jbuilder', '~> 2.5'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

gem 'shopify_app'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

Expand Down
38 changes: 38 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,19 @@ GEM
globalid (>= 0.3.6)
activemodel (5.1.2)
activesupport (= 5.1.2)
activemodel-serializers-xml (1.0.1)
activemodel (> 5.x)
activerecord (> 5.x)
activesupport (> 5.x)
builder (~> 3.1)
activerecord (5.1.2)
activemodel (= 5.1.2)
activesupport (= 5.1.2)
arel (~> 8.0)
activeresource (5.0.0)
activemodel (>= 5.0, < 6)
activemodel-serializers-xml (~> 1.0)
activesupport (>= 5.0, < 6)
activesupport (5.1.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (~> 0.7)
Expand Down Expand Up @@ -63,13 +72,17 @@ GEM
concurrent-ruby (1.0.5)
erubi (1.6.1)
execjs (2.7.0)
faraday (0.9.2)
multipart-post (>= 1.2, < 3)
ffi (1.9.18)
globalid (0.4.0)
activesupport (>= 4.2.0)
hashie (3.5.6)
i18n (0.8.6)
jbuilder (2.7.0)
activesupport (>= 4.2.0)
multi_json (>= 1.2)
jwt (1.5.1)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand All @@ -85,9 +98,26 @@ GEM
mini_portile2 (2.2.0)
minitest (5.10.2)
multi_json (1.12.1)
multi_xml (0.6.0)
multipart-post (2.0.0)
nio4r (2.1.0)
nokogiri (1.8.0)
mini_portile2 (~> 2.2.0)
oauth2 (1.1.0)
faraday (>= 0.8, < 0.10)
jwt (~> 1.0, < 1.5.2)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (>= 1.2, < 3)
omniauth (1.6.1)
hashie (>= 3.4.6, < 3.6.0)
rack (>= 1.6.2, < 3)
omniauth-oauth2 (1.4.0)
oauth2 (~> 1.0)
omniauth (~> 1.2)
omniauth-shopify-oauth2 (1.1.17)
oauth2 (~> 1.1.0)
omniauth-oauth2 (~> 1.2)
pg (0.21.0)
public_suffix (2.0.5)
puma (3.9.1)
Expand Down Expand Up @@ -137,6 +167,13 @@ GEM
selenium-webdriver (3.4.4)
childprocess (~> 0.5)
rubyzip (~> 1.0)
shopify_api (4.9.0)
activeresource (>= 3.0.0)
rack
shopify_app (7.2.11)
omniauth-shopify-oauth2 (~> 1.1.11)
rails (>= 5.0.0)
shopify_api (>= 4.3.2)
spring (2.0.2)
activesupport (>= 4.2)
spring-watcher-listen (2.0.1)
Expand Down Expand Up @@ -184,6 +221,7 @@ DEPENDENCIES
rails (~> 5.1.1)
sass-rails (~> 5.0)
selenium-webdriver
shopify_app
spring
spring-watcher-listen (~> 2.0.0)
turbolinks (~> 5)
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class HomeController < ShopifyApp::AuthenticatedController
def index
@products = ShopifyAPI::Product.find(:all, params: { limit: 10 })
end
end
4 changes: 4 additions & 0 deletions app/models/shop.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Shop < ActiveRecord::Base
include ShopifyApp::Shop
include ShopifyApp::SessionStorage
end
18 changes: 18 additions & 0 deletions app/views/home/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<% content_for :javascript do %>
<script type="text/javascript">
ShopifyApp.ready(function(){
ShopifyApp.Bar.initialize({
title: "Home",
icon: "<%= asset_path('favicon.ico') %>"
});
});
</script>
<% end %>

<h2>Products</h2>

<ul>
<% @products.each do |product| %>
<li><%= link_to product.title, "https://#{@shop_session.url}/admin/products/#{product.id}", target: "_top" %></li>
<% end %>
</ul>
15 changes: 15 additions & 0 deletions app/views/layouts/_flash_messages.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<% content_for :javascript do %>
<script type="text/javascript">
var eventName = typeof(Turbolinks) !== 'undefined' ? 'page:change' : 'DOMContentLoaded';

document.addEventListener(eventName, function() {
<% if flash[:notice] %>
ShopifyApp.flashNotice("<%= j flash[:notice].html_safe %>");
<% end %>

<% if flash[:error] %>
ShopifyApp.flashError("<%= j flash[:error].html_safe %>");
<% end %>
});
</script>
<% end %>
40 changes: 40 additions & 0 deletions app/views/layouts/embedded_app.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<% application_name = ShopifyApp.configuration.application_name %>
<title><%= application_name %></title>
<%= stylesheet_link_tag 'application' %>
<%= javascript_include_tag 'application', "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>

<body>
<div class="app-wrapper">
<div class="app-content">
<main role="main">
<%= yield %>
</main>
</div>
</div>

<%= render 'layouts/flash_messages' %>

<script src="https://cdn.shopify.com/s/assets/external/app.js?<%= Time.now.strftime('%Y%m%d%H') %>"></script>

<script type="text/javascript">
ShopifyApp.init({
apiKey: "<%= ShopifyApp.configuration.api_key %>",
shopOrigin: "<%= "https://#{ @shop_session.url }" if @shop_session %>",
debug: <%= Rails.env.development? ? 'true' : 'false' %>,
forceRedirect: true
});
</script>

<% if content_for?(:javascript) %>
<div id="ContentForJavascript" data-turbolinks-temporary>
<%= yield :javascript %>
</div>
<% end %>
</body>
</html>
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

module WatsonApp
class Application < Rails::Application
config.action_dispatch.default_headers['P3P'] = 'CP="Not used"'
config.action_dispatch.default_headers.delete('X-Frame-Options')
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1

Expand Down
6 changes: 6 additions & 0 deletions config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Rails.application.config.middleware.use OmniAuth::Builder do
provider :shopify,
ShopifyApp.configuration.api_key,
ShopifyApp.configuration.secret,
scope: ShopifyApp.configuration.scope
end
7 changes: 7 additions & 0 deletions config/initializers/shopify_app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ShopifyApp.configure do |config|
config.application_name = "My Shopify App"
config.api_key = "5857c42a164d7b0fb38ef5464bd9ec0e"
config.secret = "c23568d568a5e83d7fbcfc6addba3367"
config.scope = "read_orders, read_products"
config.embedded_app = true
end
9 changes: 9 additions & 0 deletions config/initializers/shopify_session_repository.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
if Rails.configuration.cache_classes
ShopifyApp::SessionRepository.storage = Shop
else
reloader = defined?(ActiveSupport::Reloader) ? ActiveSupport::Reloader : ActionDispatch::Reloader

reloader.to_prepare do
ShopifyApp::SessionRepository.storage = Shop
end
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Rails.application.routes.draw do
root :to => 'home#index'
mount ShopifyApp::Engine, at: '/'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
15 changes: 15 additions & 0 deletions db/migrate/20170714163850_create_shops.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class CreateShops < ActiveRecord::Migration[5.1]
def self.up
create_table :shops do |t|
t.string :shopify_domain, null: false
t.string :shopify_token, null: false
t.timestamps
end

add_index :shops, :shopify_domain, unique: true
end

def self.down
drop_table :shops
end
end
3 changes: 3 additions & 0 deletions test/fixtures/shops.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
regular_shop:
shopify_domain: 'regular-shop.myshopify.com'
shopify_token: 'token'

0 comments on commit 200537e

Please sign in to comment.