Skip to content

Commit c51a9f4

Browse files
committed
Initial commit of new Rails app, including Profile operations. Profile operations tests passing.
1 parent 918cfa7 commit c51a9f4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+1317
-2
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
*.rbc
22
capybara-*.html
33
.rspec
4-
/log
4+
/log/*
5+
!/log/.keep
56
/tmp
67
/db/*.sqlite3
78
/db/*.sqlite3-journal
@@ -25,6 +26,8 @@ config/secrets.yml
2526

2627
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
2728
.rvmrc
29+
.ruby-version
30+
.ruby-gemset
2831

2932
# if using bower-rails ignore default bower_components path bower.json files
3033
/vendor/assets/bower_components

Gemfile

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'rails', '4.2.4'
4+
gem 'sqlite3', '1.3.11'
5+
gem 'sass-rails', '~> 5.0'
6+
gem 'uglifier', '>= 1.3.0'
7+
gem 'jquery-rails', '4.0.5'
8+
gem 'file_validators', '2.0.2'
9+
gem 'trailblazer-rails', '0.1.6'
10+
gem 'cells', '4.0.2'
11+
gem 'cells-erb', '0.0.6'
12+
gem 'carrierwave', '0.10.0'
13+
gem 'encrypted_objects', git: 'https://github.com/sauy7/encrypted_objects.git'
14+
gem 'rmagick', '2.15.4'
15+
16+
group :development, :test do
17+
gem 'byebug', '8.0.0'
18+
end
19+
20+
group :development do
21+
gem 'quiet_assets', '1.1.0'
22+
gem 'spring', '1.4.0'
23+
gem 'web-console', '~> 2.0'
24+
end
25+
26+
group :test do
27+
gem 'database_cleaner'
28+
gem 'minitest-spec-rails', '5.3.0'
29+
end

Gemfile.lock

+189
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
GIT
2+
remote: https://github.com/sauy7/encrypted_objects.git
3+
revision: 0b413c754cd9937d51a8647b23eddcfe3de95fde
4+
specs:
5+
encrypted_objects (1.0.0)
6+
multi_json (~> 1.0)
7+
8+
GEM
9+
remote: https://rubygems.org/
10+
specs:
11+
actionmailer (4.2.4)
12+
actionpack (= 4.2.4)
13+
actionview (= 4.2.4)
14+
activejob (= 4.2.4)
15+
mail (~> 2.5, >= 2.5.4)
16+
rails-dom-testing (~> 1.0, >= 1.0.5)
17+
actionpack (4.2.4)
18+
actionview (= 4.2.4)
19+
activesupport (= 4.2.4)
20+
rack (~> 1.6)
21+
rack-test (~> 0.6.2)
22+
rails-dom-testing (~> 1.0, >= 1.0.5)
23+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
24+
actionview (4.2.4)
25+
activesupport (= 4.2.4)
26+
builder (~> 3.1)
27+
erubis (~> 2.7.0)
28+
rails-dom-testing (~> 1.0, >= 1.0.5)
29+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
30+
activejob (4.2.4)
31+
activesupport (= 4.2.4)
32+
globalid (>= 0.3.0)
33+
activemodel (4.2.4)
34+
activesupport (= 4.2.4)
35+
builder (~> 3.1)
36+
activerecord (4.2.4)
37+
activemodel (= 4.2.4)
38+
activesupport (= 4.2.4)
39+
arel (~> 6.0)
40+
activesupport (4.2.4)
41+
i18n (~> 0.7)
42+
json (~> 1.7, >= 1.7.7)
43+
minitest (~> 5.1)
44+
thread_safe (~> 0.3, >= 0.3.4)
45+
tzinfo (~> 1.1)
46+
arel (6.0.3)
47+
binding_of_caller (0.7.2)
48+
debug_inspector (>= 0.0.1)
49+
builder (3.2.2)
50+
byebug (8.0.0)
51+
carrierwave (0.10.0)
52+
activemodel (>= 3.2.0)
53+
activesupport (>= 3.2.0)
54+
json (>= 1.7)
55+
mime-types (>= 1.16)
56+
cells (4.0.2)
57+
tilt (>= 1.4, < 3)
58+
uber (~> 0.0.9)
59+
cells-erb (0.0.6)
60+
cells (~> 4.0.0.beta6)
61+
erbse (>= 0.0.2)
62+
database_cleaner (1.5.1)
63+
debug_inspector (0.0.2)
64+
disposable (0.1.14)
65+
representable (>= 2.2.3, < 3.0.0)
66+
uber
67+
erbse (0.0.2)
68+
erubis (2.7.0)
69+
execjs (2.6.0)
70+
file_validators (2.0.2)
71+
activemodel (>= 3.0)
72+
mime-types (>= 1.0)
73+
globalid (0.3.6)
74+
activesupport (>= 4.1.0)
75+
i18n (0.7.0)
76+
jquery-rails (4.0.5)
77+
rails-dom-testing (~> 1.0)
78+
railties (>= 4.2.0)
79+
thor (>= 0.14, < 2.0)
80+
json (1.8.3)
81+
loofah (2.0.3)
82+
nokogiri (>= 1.5.9)
83+
mail (2.6.3)
84+
mime-types (>= 1.16, < 3)
85+
mime-types (2.6.2)
86+
mini_portile (0.6.2)
87+
minitest (5.8.2)
88+
minitest-spec-rails (5.3.0)
89+
minitest (~> 5.0)
90+
rails (>= 4.1)
91+
multi_json (1.11.2)
92+
nokogiri (1.6.6.2)
93+
mini_portile (~> 0.6.0)
94+
quiet_assets (1.1.0)
95+
railties (>= 3.1, < 5.0)
96+
rack (1.6.4)
97+
rack-test (0.6.3)
98+
rack (>= 1.0)
99+
rails (4.2.4)
100+
actionmailer (= 4.2.4)
101+
actionpack (= 4.2.4)
102+
actionview (= 4.2.4)
103+
activejob (= 4.2.4)
104+
activemodel (= 4.2.4)
105+
activerecord (= 4.2.4)
106+
activesupport (= 4.2.4)
107+
bundler (>= 1.3.0, < 2.0)
108+
railties (= 4.2.4)
109+
sprockets-rails
110+
rails-deprecated_sanitizer (1.0.3)
111+
activesupport (>= 4.2.0.alpha)
112+
rails-dom-testing (1.0.7)
113+
activesupport (>= 4.2.0.beta, < 5.0)
114+
nokogiri (~> 1.6.0)
115+
rails-deprecated_sanitizer (>= 1.0.1)
116+
rails-html-sanitizer (1.0.2)
117+
loofah (~> 2.0)
118+
railties (4.2.4)
119+
actionpack (= 4.2.4)
120+
activesupport (= 4.2.4)
121+
rake (>= 0.8.7)
122+
thor (>= 0.18.1, < 2.0)
123+
rake (10.4.2)
124+
reform (2.0.5)
125+
disposable (~> 0.1.11)
126+
uber (~> 0.0.11)
127+
representable (2.3.0)
128+
uber (~> 0.0.7)
129+
rmagick (2.15.4)
130+
sass (3.4.19)
131+
sass-rails (5.0.4)
132+
railties (>= 4.0.0, < 5.0)
133+
sass (~> 3.1)
134+
sprockets (>= 2.8, < 4.0)
135+
sprockets-rails (>= 2.0, < 4.0)
136+
tilt (>= 1.1, < 3)
137+
spring (1.4.0)
138+
sprockets (3.4.0)
139+
rack (> 1, < 3)
140+
sprockets-rails (2.3.3)
141+
actionpack (>= 3.0)
142+
activesupport (>= 3.0)
143+
sprockets (>= 2.8, < 4.0)
144+
sqlite3 (1.3.11)
145+
thor (0.19.1)
146+
thread_safe (0.3.5)
147+
tilt (2.0.1)
148+
trailblazer (1.0.4)
149+
reform (>= 1.2.0)
150+
uber (>= 0.0.15)
151+
trailblazer-rails (0.1.6)
152+
trailblazer (>= 1.0.4)
153+
tzinfo (1.2.2)
154+
thread_safe (~> 0.1)
155+
uber (0.0.15)
156+
uglifier (2.7.2)
157+
execjs (>= 0.3.0)
158+
json (>= 1.8.0)
159+
web-console (2.2.1)
160+
activemodel (>= 4.0)
161+
binding_of_caller (>= 0.7.2)
162+
railties (>= 4.0)
163+
sprockets-rails (>= 2.0, < 4.0)
164+
165+
PLATFORMS
166+
ruby
167+
168+
DEPENDENCIES
169+
byebug (= 8.0.0)
170+
carrierwave (= 0.10.0)
171+
cells (= 4.0.2)
172+
cells-erb (= 0.0.6)
173+
database_cleaner
174+
encrypted_objects!
175+
file_validators (= 2.0.2)
176+
jquery-rails (= 4.0.5)
177+
minitest-spec-rails (= 5.3.0)
178+
quiet_assets (= 1.1.0)
179+
rails (= 4.2.4)
180+
rmagick (= 2.15.4)
181+
sass-rails (~> 5.0)
182+
spring (= 1.4.0)
183+
sqlite3 (= 1.3.11)
184+
trailblazer-rails (= 0.1.6)
185+
uglifier (>= 1.3.0)
186+
web-console (~> 2.0)
187+
188+
BUNDLED WITH
189+
1.10.6

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
# carrierwave-trailblazer
1+
# carrierwave_trailblazer_rails_app
2+
23
Rails app demonstrating use of Carrierwave for file uploads with Trailblazer operations

Rakefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Add your own tasks in files placed in lib/tasks ending in .rake,
2+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3+
4+
require File.expand_path('../config/application', __FILE__)
5+
6+
Rails.application.load_tasks

app/assets/images/.keep

Whitespace-only changes.

app/assets/javascripts/application.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// This is a manifest file that'll be compiled into application.js, which will include all the files
2+
// listed below.
3+
//
4+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6+
//
7+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8+
// compiled file.
9+
//
10+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11+
// about supported directives.
12+
//
13+
//= require jquery
14+
//= require jquery_ujs
15+
//= require_tree .
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* This is a manifest file that'll be compiled into application.css, which will include all the files
3+
* listed below.
4+
*
5+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7+
*
8+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
9+
* compiled file so the styles you add here take precedence over styles defined in any styles
10+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11+
* file per style scope.
12+
*
13+
*= require_tree .
14+
*= require_self
15+
*/

app/concepts/profile/cell.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Profile::Cell < Cell::Concept
2+
def show
3+
render
4+
end
5+
end

app/concepts/profile/crud.rb

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
require 'carrierwave/mount'
2+
require 'encrypted_objects'
3+
4+
class Profile < ActiveRecord::Base
5+
class Create < Trailblazer::Operation
6+
include EncryptedObjects
7+
include Model
8+
model Profile, :create
9+
10+
contract do
11+
extend CarrierWave::Mount
12+
mount_uploader :avatar, AvatarUploader
13+
14+
property :file, virtual: true
15+
property :file_cache, virtual: true
16+
property :remove_file, virtual: true
17+
property :display_name
18+
19+
validates :display_name, presence: true
20+
validates :file, file_size: { less_than: 10.kilobytes },
21+
file_content_type: { allow: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'] }
22+
end
23+
24+
def process(params)
25+
validate(params[:profile]) do |f|
26+
f.save
27+
store_file!(f)
28+
return
29+
end
30+
retain_file!(contract)
31+
end
32+
33+
private
34+
35+
def file_errors(contract)
36+
contract.errors.messages.fetch(:file){[]}
37+
end
38+
39+
def retain_file!(contract)
40+
return if file_errors(contract).present?
41+
42+
if contract.file.present?
43+
contract.avatar = contract.file
44+
contract.avatar.cache!
45+
contract.file_cache = json_b64_encode(
46+
{ cache_name: contract.avatar.cache_name,
47+
original_filename: contract.file.original_filename }
48+
)
49+
end
50+
end
51+
52+
def persist_file!(contract)
53+
if contract.file.present?
54+
contract.avatar = contract.file
55+
elsif contract.file_cache.present?
56+
contract.avatar.retrieve_from_cache!(json_b64_decode(contract.file_cache)['cache_name'])
57+
end
58+
contract.avatar.store!
59+
contract.model.update_attribute(:avatar, contract.avatar.filename)
60+
end
61+
62+
def store_file!(contract)
63+
return unless contract.file.present? || contract.file_cache.present?
64+
persist_file!(contract)
65+
end
66+
end
67+
68+
class Update < Create
69+
action :update
70+
71+
def process(params)
72+
remove_file!(params[:profile]) # before_validation on_update
73+
super
74+
end
75+
76+
private
77+
78+
def remove_stored_file!(contract)
79+
contract.avatar.retrieve_from_store!(contract.model.avatar)
80+
contract.avatar.remove!
81+
end
82+
83+
def remove_file!(profile_params)
84+
return unless profile_params.fetch(:remove_file){'0'} == '1'
85+
remove_stored_file!(contract)
86+
contract.model.update_attribute(:avatar, nil)
87+
end
88+
89+
def remove_previous_file!(contract)
90+
return unless contract.model.avatar.present?
91+
remove_stored_file!(contract)
92+
end
93+
94+
def store_file!(contract)
95+
return unless contract.file.present? || contract.file_cache.present?
96+
remove_previous_file!(contract)
97+
persist_file!(contract)
98+
end
99+
end
100+
101+
class Delete < Update
102+
def process(params)
103+
remove_stored_file!(contract)
104+
model.destroy
105+
end
106+
end
107+
end

0 commit comments

Comments
 (0)