simple_images 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/.rspec +1 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +146 -0
- data/Rakefile +6 -0
- data/app/assets/javascripts/simple_images.js.coffee +10 -0
- data/app/assets/stylesheets/simple_images.css.scss +23 -0
- data/app/controllers/.keep +0 -0
- data/app/controllers/simple_images_controller.rb +59 -0
- data/app/helpers/.keep +0 -0
- data/app/helpers/simple_images_helper.rb +20 -0
- data/app/models/.keep +0 -0
- data/app/views/.keep +0 -0
- data/app/views/simple_images/_destroy.html.erb +7 -0
- data/app/views/simple_images/_edit.html.erb +6 -0
- data/app/views/simple_images/_form.html.erb +6 -0
- data/config/initializers/dragonfly.rb +2 -0
- data/lib/generators/simple_images_generator.rb +17 -0
- data/lib/generators/templates/create_simple_images.rb +17 -0
- data/lib/generators/templates/initializer.rb +7 -0
- data/lib/generators/templates/simple_image.rb +3 -0
- data/lib/simple_images.rb +8 -0
- data/lib/simple_images/config.rb +13 -0
- data/lib/simple_images/engine.rb +9 -0
- data/lib/simple_images/imageable_additions.rb +21 -0
- data/lib/simple_images/simple_image_additions.rb +16 -0
- data/lib/simple_images/version.rb +3 -0
- data/simple_images.gemspec +34 -0
- data/spec/assets/images/test.png +0 -0
- data/spec/assets/images/test2.png +0 -0
- data/spec/controllers/simple_images_controller_spec.rb +134 -0
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/javascripts/application.js +13 -0
- data/spec/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/controllers/concerns/.keep +0 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/mailers/.keep +0 -0
- data/spec/dummy/app/models/.keep +0 -0
- data/spec/dummy/app/models/concerns/.keep +0 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +23 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +5 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +29 -0
- data/spec/dummy/config/environments/production.rb +80 -0
- data/spec/dummy/config/environments/test.rb +36 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/dragonfly.rb +1 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/secret_token.rb +12 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +5 -0
- data/spec/dummy/lib/assets/.keep +0 -0
- data/spec/dummy/public/404.html +58 -0
- data/spec/dummy/public/422.html +58 -0
- data/spec/dummy/public/500.html +57 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/system/dragonfly/test/test.png +0 -0
- data/spec/extra/models.rb +7 -0
- data/spec/extra/schema.rb +19 -0
- data/spec/factories/articles.rb +6 -0
- data/spec/factories/simple_images.rb +5 -0
- data/spec/helpers/simple_images_helper_spec.rb +46 -0
- data/spec/models/imageable_spec.rb +5 -0
- data/spec/models/simple_image_spec.rb +12 -0
- data/spec/spec_helper.rb +30 -0
- metadata +340 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9b3dc449cf37a7a5a4352d2c692288c9609df0fa
|
4
|
+
data.tar.gz: e7bdc243c1e647e83c71372462e87c386ebef926
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6f720eb92259e03e0204c34c848e2259aecdbb40063a63d2893db9e2e07d9450ff792995c6cc56bd6c977fac2c7fa54c439a4a9a34b9d6518004af15f829b0df
|
7
|
+
data.tar.gz: d7873f7f19b8d206bfefd3bf60759e8feecb440558cf4595ade740a3610808eaf43fbe4b99b60717df118a1f3aedcdc421e6e8af43307a85db11de4cde484b03
|
data/.gitignore
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
spec/dummy/log/
|
18
|
+
tmp
|
19
|
+
.project
|
20
|
+
spec/dummy/public/system/dragonfly/test/**/
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Kainage
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
# Simple Images
|
2
|
+
|
3
|
+
Add simple one-click image uploading to active record models.
|
4
|
+
|
5
|
+
**Requires ruby >= 1.9.3**
|
6
|
+
|
7
|
+
**Requires rails >= 4.0.0**
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'simple_images'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
```
|
20
|
+
$ bundle
|
21
|
+
```
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
```
|
26
|
+
$ gem install simple_images
|
27
|
+
```
|
28
|
+
|
29
|
+
Run the generator to create the migration, initializer and simple_image model files:
|
30
|
+
|
31
|
+
```
|
32
|
+
$ rails g simple_images
|
33
|
+
```
|
34
|
+
|
35
|
+
Migrate the database:
|
36
|
+
|
37
|
+
```
|
38
|
+
$ rake db:migrate
|
39
|
+
```
|
40
|
+
|
41
|
+
## Usage
|
42
|
+
|
43
|
+
### Image Model
|
44
|
+
|
45
|
+
This will be generated and placed in your app/models folder:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
class SimpleImage
|
49
|
+
include SimpleImages::ImageAdditions
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
You can add any custom functionality to the Message model here.
|
54
|
+
|
55
|
+
### Imageable Model
|
56
|
+
|
57
|
+
Add the appropriate line to the top of your activerecord model:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
class Article
|
61
|
+
has_simple_images
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
Add the routes for the resource as well:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
resources :articles
|
69
|
+
resources :simple_images, shallow: true, only: [:create, :update, :destroy]
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
The class is not restricted to Article, it can be any class you add ```has_simple_images``` to.
|
74
|
+
|
75
|
+
### Configuration
|
76
|
+
|
77
|
+
In the created initializer:
|
78
|
+
|
79
|
+
```
|
80
|
+
config/initializers/simple_images.rb
|
81
|
+
```
|
82
|
+
|
83
|
+
You will find some configuration options availible.
|
84
|
+
Edit this file and restart you server to have the changes take effect.
|
85
|
+
|
86
|
+
### User and CanCan Integration
|
87
|
+
|
88
|
+
If you have a ```current_user``` it will assign the created image to that user.
|
89
|
+
Otherwise it will be nil.
|
90
|
+
|
91
|
+
If you are using cancan it will pick that up for the 3 controller methods and
|
92
|
+
check authorization respectively
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
authorize! :create, @simple_image
|
96
|
+
authorize! :update, @simple_image
|
97
|
+
authorize! :destroy, @simple_image
|
98
|
+
```
|
99
|
+
|
100
|
+
### Helpers
|
101
|
+
|
102
|
+
The point of this gem is to have dead simple image uploading anywhere in your app.
|
103
|
+
|
104
|
+
An image must be associated to an active record model. If you want all images to be
|
105
|
+
availible to all things just pick an arbitrary object that you are not going to
|
106
|
+
destroy (such as first user) and associate all images with that record.
|
107
|
+
|
108
|
+
Include the javascript file in your application.js manifest:
|
109
|
+
|
110
|
+
```
|
111
|
+
//= require simple_images
|
112
|
+
```
|
113
|
+
|
114
|
+
Include the stylesheet in your application.css manifest:
|
115
|
+
|
116
|
+
```
|
117
|
+
/*
|
118
|
+
*= require simple_images
|
119
|
+
*/
|
120
|
+
```
|
121
|
+
|
122
|
+
You can omit the stylesheet if you want to style your own upload button from scratch
|
123
|
+
|
124
|
+
In your views you can call the following methods:
|
125
|
+
|
126
|
+
```
|
127
|
+
<%= render_simple_images_edit_link_for(image) %>
|
128
|
+
<%= render_simple_images_destroy_link_for(image) %>
|
129
|
+
<%= render_simple_images_actions_for(image) %>
|
130
|
+
<%= render_simple_images_form_for(imageable) %>
|
131
|
+
```
|
132
|
+
|
133
|
+
which should be pretty straight forward.
|
134
|
+
|
135
|
+
### Creating Images
|
136
|
+
|
137
|
+
Simply click on the 'Add Image' Button and select a file to upload.
|
138
|
+
If your controller responds to ```current_user``` then it will be assigned to the image.
|
139
|
+
|
140
|
+
## Contributing
|
141
|
+
|
142
|
+
1. Fork it
|
143
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
144
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
145
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
146
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
.new_simple_image {
|
2
|
+
height: 45px;
|
3
|
+
width: 45px;
|
4
|
+
margin: 5px;
|
5
|
+
padding: 10px;
|
6
|
+
background: silver;
|
7
|
+
position: relative;
|
8
|
+
font-size: 16px;
|
9
|
+
font-family: Arial, Helvetica, Sans-serif;
|
10
|
+
.simple_image_btn {
|
11
|
+
border: 1px dashed white;
|
12
|
+
height: 23px;
|
13
|
+
width: 43px;
|
14
|
+
text-align: center;
|
15
|
+
color: white;
|
16
|
+
padding: 10px 0;
|
17
|
+
font-size: 0.7em;
|
18
|
+
cursor: pointer;
|
19
|
+
&:hover {
|
20
|
+
background: grey;
|
21
|
+
}
|
22
|
+
}
|
23
|
+
}
|
File without changes
|
@@ -0,0 +1,59 @@
|
|
1
|
+
class SimpleImagesController < ApplicationController
|
2
|
+
before_filter :find_imageable, except: :index
|
3
|
+
|
4
|
+
def create
|
5
|
+
@simple_image = @imageable.simple_images.build(simple_image_params)
|
6
|
+
@simple_image.user_id = current_user.id if respond_to?(:current_user)
|
7
|
+
|
8
|
+
authorize! :create, @simple_image if defined?(CanCan::Ability)
|
9
|
+
|
10
|
+
respond_to do |format|
|
11
|
+
if @simple_image.save
|
12
|
+
format.html { redirect_to request.referrer, notice: 'Image was successfully created.' }
|
13
|
+
format.json { render json: @simple_image, status: :created, location: @simple_image }
|
14
|
+
else
|
15
|
+
format.html { redirect_to request.referrer, alert: @simple_image.errors.full_messages.join(', ') }
|
16
|
+
format.json { render json: @simple_image.errors, status: :unprocessable_entity }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def update
|
22
|
+
@simple_image = SimpleImage.find(params[:id])
|
23
|
+
authorize! :update, @simple_image if defined?(CanCan::Ability)
|
24
|
+
|
25
|
+
respond_to do |format|
|
26
|
+
if @simple_image.update(simple_image_params)
|
27
|
+
format.html { redirect_to request.referrer, notice: 'Image was successfully updated.' }
|
28
|
+
format.json { head :no_content }
|
29
|
+
else
|
30
|
+
format.html { redirect_to request.referrer, alert: @simple_image.errors.full_messages.join(', ') }
|
31
|
+
format.json { render json: @simple_image.errors, status: :unprocessable_entity }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def destroy
|
37
|
+
@simple_image = SimpleImage.find(params[:id])
|
38
|
+
authorize! :destroy, @simple_image if defined?(CanCan::Ability)
|
39
|
+
|
40
|
+
@imageable = @simple_image.imageable
|
41
|
+
@simple_image.destroy
|
42
|
+
|
43
|
+
respond_to do |format|
|
44
|
+
format.html { redirect_to request.referrer, notice: 'Image was permanently removed.' }
|
45
|
+
format.json { head :no_content }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def simple_image_params
|
52
|
+
params.require(:simple_image).permit(:image)
|
53
|
+
end
|
54
|
+
|
55
|
+
def find_imageable
|
56
|
+
resource, id = request.path.split('/')[1, 2]
|
57
|
+
@imageable = resource.singularize.classify.constantize.find(id)
|
58
|
+
end
|
59
|
+
end
|
data/app/helpers/.keep
ADDED
File without changes
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module SimpleImagesHelper
|
2
|
+
def render_simple_images_edit_link_for(image)
|
3
|
+
render 'simple_images/edit', image: image
|
4
|
+
end
|
5
|
+
|
6
|
+
def render_simple_images_destroy_link_for(image)
|
7
|
+
render 'simple_images/destroy', image: image
|
8
|
+
end
|
9
|
+
|
10
|
+
def render_simple_images_actions_for(image)
|
11
|
+
[].tap do |ary|
|
12
|
+
ary << render_simple_images_edit_link_for(image)
|
13
|
+
ary << render_simple_images_destroy_link_for(image)
|
14
|
+
end.join.html_safe
|
15
|
+
end
|
16
|
+
|
17
|
+
def render_simple_images_form_for(imageable)
|
18
|
+
render 'simple_images/form', imageable: imageable
|
19
|
+
end
|
20
|
+
end
|
data/app/models/.keep
ADDED
File without changes
|
data/app/views/.keep
ADDED
File without changes
|
@@ -0,0 +1,6 @@
|
|
1
|
+
<%= form_for [main_app, imageable, SimpleImage.new], :html => { :multipart => true } do |f| %>
|
2
|
+
<%= content_tag :div, 'Add Image', :class => 'simple_image_btn' %>
|
3
|
+
<div style='display: none'>
|
4
|
+
<%= f.file_field :image, :class => 'simple_image_input' %>
|
5
|
+
</div>
|
6
|
+
<% end %>
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rails/generators/migration'
|
2
|
+
|
3
|
+
class SimpleImagesGenerator < Rails::Generators::Base
|
4
|
+
include Rails::Generators::Migration
|
5
|
+
|
6
|
+
source_root File.expand_path("../templates", __FILE__)
|
7
|
+
|
8
|
+
def self.next_migration_number(path)
|
9
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_simple_images_files
|
13
|
+
copy_file "simple_image.rb", "app/models/simple_image.rb"
|
14
|
+
copy_file "initializer.rb", "config/initializers/simple_images.rb"
|
15
|
+
migration_template "create_simple_images.rb", "db/migrate/create_simple_images.rb"
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class CreateSimpleImages < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :simple_images do |t|
|
4
|
+
t.references :imageable, polymorphic: true, null: false
|
5
|
+
t.integer :user_id
|
6
|
+
t.string :image_uid, null: false
|
7
|
+
t.string :image_name, null: false
|
8
|
+
t.boolean :active, null: false, default: true
|
9
|
+
t.boolean :visible, null: false, default: true
|
10
|
+
|
11
|
+
t.timestamps
|
12
|
+
end
|
13
|
+
|
14
|
+
add_index :simple_images, [:imageable_id, :imageable_type]
|
15
|
+
# add_index :simple_images, :user_id # Uncomment this if using user_id's
|
16
|
+
end
|
17
|
+
end
|