sinatra-backstage 0.0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8994f18d2816cd1b60ab0d0eaaf902fc3257734d
4
- data.tar.gz: 96cf5456d3f53b05660fa121f6764ab1ba42cb48
3
+ metadata.gz: c9a0c11c57f8f5fff03e397588ad03308f731aa9
4
+ data.tar.gz: 2781b76ba12acc5952eb021c1414b2cd1fdf4903
5
5
  SHA512:
6
- metadata.gz: 78f4facabf69816443541ed85e4a75d4221d83e3af0a1206eb980456f677222b9bbef158cd054078877138641e38dd053669ce571189fc31405d036def6dea09
7
- data.tar.gz: 66d9b80b2c7f6340e6950cf371f5c094d9af0277c873d26efc9a17ea5a4cf35c020c3ff2ad67174d62ff6e2fc653b5d3540ca7e632e9b90d541dc782114a5e9f
6
+ metadata.gz: 31210c12c34db4f36c08a325ea392d2878a9af73c9745ae318d50d4bcd0d06f513a86a518dc209ef02d479e042666ae3534d7483292e94bc7bba4275b2a16b19
7
+ data.tar.gz: 8a17806c06e9f17778e4881e781e984980276868a6a6dd74e4c3357f539cf0ed9223619a7a83ab436663cecd7777cdb22d9ca4001219eaadf8afab5a96197c77
@@ -1,5 +1 @@
1
- Dir.glob(File.join(__dir__, 'rest', '*.rb')).each {|file| require file }
2
-
3
- # module Sinatra
4
- # helpers Sinatra::Backstage::REST
5
- # end
1
+ Dir.glob(File.join(__dir__, 'rest', '*.rb')).each {|file| require file }
@@ -0,0 +1 @@
1
+ Dir.glob(File.join(__dir__, 'stored_file', '*.rb')).each {|file| require file }
@@ -0,0 +1,18 @@
1
+ module Sinatra
2
+ module Backstage
3
+ module StoredFile
4
+ module Helper
5
+
6
+ def get_attachment_attrs(attachment)
7
+ {
8
+ :filename => attachment[:filename],
9
+ :filesize => File.size(attachment[:tempfile].path)
10
+ }
11
+ end
12
+
13
+ end
14
+
15
+ include Helper
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,91 @@
1
+ ## database gems
2
+ require 'dm-core'
3
+ require 'dm-types'
4
+ require 'dm-validations'
5
+ require 'dm-timestamps'
6
+
7
+ ## Stored File class (DataMapper Resource)
8
+ module Sinatra
9
+ module Backstage
10
+ module StoredFile
11
+ class StoredFile
12
+
13
+ URL_DIR = "/files"
14
+ DIR = "/public"
15
+
16
+ include DataMapper::Resource
17
+
18
+ ## Properties
19
+ property :id, Serial
20
+ property :filename, String, :required => true
21
+ property :filesize, Integer, :required => true, :default => 0
22
+ property :created_at, DateTime, :default => DateTime.now
23
+
24
+ default_scope(:default).update(:order => [:created_at.desc])
25
+
26
+ def filesize_kb
27
+ "#{filesize/1024}"
28
+ end
29
+
30
+ def uploaded
31
+ "#{created_at.strftime('%d %b')}"
32
+ end
33
+
34
+ def self.stored_dir
35
+ "#{self::DIR + self::URL_DIR}"
36
+ end
37
+
38
+ def stored_filename
39
+ extname = File.extname(filename)
40
+ "#{File.basename(filename, extname)}_#{id}#{extname}"
41
+ end
42
+
43
+ def stored_path
44
+ ".#{self.class.stored_dir}/#{stored_filename}"
45
+ end
46
+
47
+ def url
48
+ "#{self.class::URL_DIR}/#{stored_filename}"
49
+ end
50
+
51
+ ## Methods
52
+ def self.create(attrs, tempfile, extra = {})
53
+ file = super( attrs.merge extra )
54
+ write_file(file, tempfile)
55
+ file
56
+ rescue DataMapper::SaveFailureError => e
57
+ puts e.resource.errors.inspect
58
+ end
59
+
60
+ def update(attrs, tempfile, extra = {})
61
+ delete_file
62
+ super( attrs.merge extra )
63
+ self.class.write_file(self, tempfile)
64
+ rescue DataMapper::SaveFailureError => e
65
+ puts e.resource.errors.inspect
66
+ end
67
+
68
+ ## Hooks
69
+ before :destroy do
70
+ delete_file
71
+ end
72
+
73
+
74
+ protected
75
+
76
+ ## Helpers
77
+ def self.write_file(stored_file, tempfile)
78
+ FileUtils.mkdir_p ".#{stored_dir}" unless Dir.exist? ".#{stored_dir}"
79
+ File.open(stored_file.stored_path, 'wb') do |f|
80
+ f.write(tempfile.read)
81
+ end
82
+ end
83
+
84
+ def delete_file
85
+ File.delete(stored_path) if File.exist?(stored_path)
86
+ end
87
+
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,60 @@
1
+ module Sinatra
2
+ module Backstage
3
+ module StoredFile
4
+ module Routing
5
+
6
+ def self.included(controller)
7
+ controller.extend self
8
+ end
9
+
10
+ def receive_attachment(klass, child_klass, namespace, field_name)
11
+
12
+ helpers Sinatra::Backstage::StoredFile::Helper
13
+
14
+ ## Add logo after create franchise
15
+ before namespace, :method => :post do
16
+ if params[:object][field_name]
17
+ begin
18
+ attachment = params[:object].delete(field_name)
19
+ attachment_attrs = get_attachment_attrs(attachment)
20
+ params[:object][field_name] = child_klass.create(
21
+ attachment_attrs,
22
+ attachment[:tempfile]
23
+ )
24
+ rescue DataMapper::SaveFailureError => e
25
+ puts e.resource.errors.inspect
26
+ end
27
+ end
28
+ end
29
+
30
+ ## Create logo object before update franchise
31
+ before "#{namespace}/:id", :method => :put do |id|
32
+ if params[:object][field_name]
33
+ begin
34
+ attachment = params[:object].delete(field_name)
35
+ attachment_attrs = get_attachment_attrs(attachment)
36
+ klass.get!(id).send(field_name).update(
37
+ attachment_attrs,
38
+ attachment[:tempfile]
39
+ )
40
+ rescue DataMapper::SaveFailureError => e
41
+ puts e.resource.errors.inspect
42
+ end
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+
50
+ def self.included(app)
51
+ app.include Routing
52
+ end
53
+
54
+ def self.registered(app)
55
+ app.register Routing
56
+ end
57
+
58
+ end
59
+ end
60
+ end
@@ -1,5 +1 @@
1
- Dir.glob(File.join(__dir__, 'user', '*.rb')).each {|file| require file }
2
-
3
- # module Sinatra
4
- # register Backstage::User
5
- # end
1
+ Dir.glob(File.join(__dir__, 'user', '*.rb')).each {|file| require file }
@@ -43,23 +43,22 @@ module Sinatra
43
43
  end
44
44
 
45
45
  def authorized?
46
- authorized_user.is_a? User
46
+ authorized_user.role != :anon
47
47
  end
48
48
 
49
49
  def authorized_user
50
50
  if @authorized_user.nil?
51
- return nil unless cookies[:username] && cookies[:session]
52
- user = settings.user_class.first :username => cookies[:username]
53
- return nil unless user
54
- @authorized_user = (user.session == cookies[:session] ? user : false)
51
+ if cookies[:username] && cookies[:session]
52
+ user = settings.user_class.first :username => cookies[:username]
53
+ if user && user.session == cookies[:session]
54
+ return @authorized_user = user
55
+ end
56
+ end
57
+ @authorized_user = settings.user_class.new
55
58
  end
56
59
  @authorized_user
57
60
  end
58
61
 
59
- def user_role
60
- authorized? ? :user : :guest
61
- end
62
-
63
62
  end
64
63
 
65
64
  include Helper
@@ -7,12 +7,12 @@ module Sinatra
7
7
  def self.registered(app)
8
8
 
9
9
  ## Helpers
10
- app.helpers Sinatra::Cookies, Backstage::User::Helper
10
+ app.helpers Sinatra::Cookies, Sinatra::Backstage::User::Helper
11
11
 
12
12
  ## Configure
13
13
  app.enable :sessions
14
14
 
15
- app.set :user_class, Backstage::User::User
15
+ app.set :user_class, Sinatra::Backstage::User::User
16
16
 
17
17
  ## Routes
18
18
  ### get login page
@@ -30,7 +30,9 @@ module Sinatra
30
30
  # halt
31
31
  @errors = []
32
32
  begin
33
- user = settings.user_class.create params[:user]
33
+ user = settings.user_class.create(
34
+ params[:user].merge({:role => :user})
35
+ )
34
36
  set_session user
35
37
  redirect '/'
36
38
  rescue DataMapper::SaveFailureError => e
@@ -41,6 +43,9 @@ module Sinatra
41
43
  end
42
44
  @errors.flatten!
43
45
  puts @errors.inspect
46
+ params[:user].delete_if do |key, val|
47
+ ['password', 'password_confirmation'].include? key
48
+ end
44
49
  end
45
50
  # puts "User::Middleware post /signup ( params['user']['email'] = #{params['user']['email']} )"
46
51
  eval "#{settings.render_engine} 'user/signup'"
@@ -28,6 +28,9 @@ module Sinatra
28
28
  # # self.password_hash = generate_salt_hash(new_password)
29
29
  # self.password_hash = new_password
30
30
  # end
31
+ property :role, Enum[:anon, :user],
32
+ :required => true,
33
+ :default => :anon
31
34
 
32
35
  def session
33
36
  # generate_salt_hash(username)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-backstage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Popov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-18 00:00:00.000000000 Z
11
+ date: 2015-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sinatra-conditions
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: dm-core
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +94,20 @@ dependencies:
80
94
  - - "~>"
81
95
  - !ruby/object:Gem::Version
82
96
  version: '1'
97
+ - !ruby/object:Gem::Dependency
98
+ name: dm-timestamps
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1'
83
111
  description: Sinatra-Backstage simplifies the use of the basic routes and models for
84
112
  Sinatra and DataMapper. Currently it includes REST and User.
85
113
  email: alex.wayfer@gmail.com
@@ -91,6 +119,10 @@ files:
91
119
  - lib/sinatra/backstage/rest.rb
92
120
  - lib/sinatra/backstage/rest/rest_model.rb
93
121
  - lib/sinatra/backstage/rest/rest_routes.rb
122
+ - lib/sinatra/backstage/stored_file.rb
123
+ - lib/sinatra/backstage/stored_file/stored_file_helper.rb
124
+ - lib/sinatra/backstage/stored_file/stored_file_model.rb
125
+ - lib/sinatra/backstage/stored_file/stored_file_routes.rb
94
126
  - lib/sinatra/backstage/user.rb
95
127
  - lib/sinatra/backstage/user/user_helper.rb
96
128
  - lib/sinatra/backstage/user/user_middleware.rb