initial copy commit

This commit is contained in:
2017-12-21 11:36:52 +01:00
commit 659da24af3
135 changed files with 3288 additions and 0 deletions
@@ -0,0 +1,2 @@
//= link_directory ../javascripts/dunlop/ember .js
//= link_directory ../stylesheets/dunlop/ember .css
@@ -0,0 +1,13 @@
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require_tree .
@@ -0,0 +1,15 @@
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
* files in this directory. Styles in this file should be added after the last require_* statement.
* It is generally better to create a new file per style scope.
*
*= require_tree .
*= require_self
*/
@@ -0,0 +1,54 @@
module Dunlop
module Ember
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :authenticate_user_from_token!
before_action :authenticate_user!
def ember_path
app_globals = {}
if adapter_name = params[:adapter].to_s.scan(/\w+/).first.presence
app_index = Rails.root.join("public/adapters/#{adapter_name}/app/index.html")
return render text: 'unauthorized, no adapter engine', status: 403 unless adapter = Panda.adapters[adapter_name]
#app_globals = adapter.settings # load through request instead. More stable then changing environments
unless Rails.application.config.try(:loose_ember_app_authorization)
return render text: 'unauthorized, no adapter engine', status: 403 unless current_user.admin? or authorize!(:read, adapter.engine)
end
else
app_index = Rails.root.join('public/app/index.html')
end
Rails.logger.info "Loading ember app for path '#/#{params[:rest]}' adapter: '#{params[:adapter].presence || 'main_app'}'"
if app_index.exist?
index_html = app_index.read.to_s
app_globals[:flash] = flash.to_h if flash.present?
index_html.sub! 'app_globals={}', "app_globals=#{app_globals.to_json}" if app_globals.present?
render html: index_html.html_safe
else
render html: "App file <tt>#{app_index}</tt> not found".html_safe
end
end
rescue_from CanCan::AccessDenied do
if request.format.json?
head 403
else
render 'application/403', status: 403
end
end
private
def authenticate_user_from_token!
# https://romulomachado.github.io/2015/09/28/using-ember-simple-auth-with-devise.html
authenticate_with_http_token do |token, options|
user_email = options[:email].presence
user = user_email && User.find_by_email(user_email)
if user && Devise.secure_compare(user.authentication_token, token)
sign_in user, store: false
end
end
end
end
end
end
@@ -0,0 +1,27 @@
class Dunlop::Ember::SessionsController < Devise::SessionsController
#respond_to :json
skip_before_action :verify_authenticity_token, if: :json_request?, raise: false
#https://www.devmynd.com/blog/2014-7-rails-ember-js-with-the-ember-cli-redux-part-1-the-api-and-cms-with-ruby-on-rails/
def create
if json_request?
super do |user|
data = {
token: user.authentication_token,
email: user.email,
user_id: user.id
}
render json: data, status: 201 and return
end
else
super
end
end
protected
def json_request?
request.format.json?
end
end
@@ -0,0 +1,16 @@
module Dunlop::Ember
class UsersController < Dunlop::Ember::ApplicationController
def me
if respond_to?(:current_user) and current_user.present?
user = current_user
sign_out user # no more current_user, needed to allow sign_in using store: true to work (devise v4.3.0)
sign_in user, store: true # create session if not exist, only needed for hybrid rails/api integrations like ePOTS. Otherwise api only without store is sufficient
#sign_in current_user, store: true # create session if not exist, only needed for hybrid rails/api integrations like ePOTS. Otherwise api only without store is sufficient
#warden.set_user current_user, store: true
render json: current_user, serializer: UserSerializer
else
head 404
end
end
end
end
@@ -0,0 +1,6 @@
module Dunlop
module Ember
module ApplicationHelper
end
end
end
+6
View File
@@ -0,0 +1,6 @@
module Dunlop
module Ember
class ApplicationJob < ActiveJob::Base
end
end
end
@@ -0,0 +1,8 @@
module Dunlop
module Ember
class ApplicationMailer < ActionMailer::Base
default from: 'from@example.com'
layout 'mailer'
end
end
end
+51
View File
@@ -0,0 +1,51 @@
# Transform a nested tructure indicated by dots to a structure as used by the ActiveRecord includes statement
# - dslams
# - ppls.distribution_cables
# =>
# [:dslams, {ppls: :distribution_cables}]
class FlatKeys
class << self
def as_nested_structure(ary)
return ary unless ary.first.is_a?(String)
nested_keys, flat_keys = ary.partition{|spec| spec['.']}
result = flat_keys.map(&:to_sym)
if nested_keys.any?
obj = {}
nested_keys.map{ |key| key.split('.').map(&:to_sym) }.each do |parts|
traverse_nest_structure(parts, obj)
end
result.push obj
end
result
end
def traverse_nest_structure(parts, obj)
key = parts.shift
if parts.size == 1
case obj[key]
when nil then obj[key] = parts[0]
when Array then obj[key] |= [parts[0]]
when Hash
raise "Colliding keys for nesting of #{key} -> #{parts[0]}" unless obj[key].has_key?(parts[0])
else # expect symbol
obj[key] = Array.wrap(obj[key]).push parts[0] unless obj[key] == parts[0] # no duplicate
end
else # parts.size > 2
case obj[key]
when nil
obj[key] = {}
traverse_nest_structure(parts, obj[key])
when Array
raise "Cannot traverse #{key} -> #{parts.join('.')} because existing array value #{obj[key].inspect}"
when Hash
traverse_nest_structure(parts, obj[key])
else # expect symbol
raise "Cannot add deeper nesting for endpoing with different name #{key} -> #{obj[key]} => #{parts.join('.')}" unless obj[key] = parts[0] # same name, allows deeper nesting
obj[key] = {obj[key] => nil} # prepare for nesting
traverse_nest_structure(parts, obj[key])
end
end
end
end
end
@@ -0,0 +1,4 @@
h3
span Could not find
tt= '/app/index.html'
span file
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>Dunlop ember</title>
<%= stylesheet_link_tag "dunlop/ember/application", media: "all" %>
<%= javascript_include_tag "dunlop/ember/application" %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>