sniffets 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.DS_Store +0 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +41 -0
- data/Rakefile +2 -0
- data/app/controllers/admin/base_controller.rb +4 -0
- data/app/controllers/admin/snippets_controller.rb +76 -0
- data/app/helpers/snippets_helper.rb +15 -0
- data/app/models/snippet.rb +21 -0
- data/app/views/admin/snippets/_form.html.erb +32 -0
- data/app/views/admin/snippets/edit.html.erb +4 -0
- data/app/views/admin/snippets/index.html.erb +34 -0
- data/app/views/admin/snippets/index.json.jbuilder +4 -0
- data/app/views/admin/snippets/new.html.erb +4 -0
- data/app/views/admin/snippets/show.html.erb +22 -0
- data/app/views/admin/snippets/show.json.jbuilder +1 -0
- data/lib/.DS_Store +0 -0
- data/lib/generators/.DS_Store +0 -0
- data/lib/generators/sniffets/install/install_generator.rb +28 -0
- data/lib/generators/sniffets/install/templates/migrations/create_snippets.rb +15 -0
- data/lib/sniffets/engine.rb +4 -0
- data/lib/sniffets/version.rb +3 -0
- data/lib/sniffets.rb +6 -0
- data/sniffets.gemspec +24 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8956bfd27585384acfa7e51315986315bd21867c
|
4
|
+
data.tar.gz: 43ea9f6414e4dfcb47d1999adc55fb2df4d003b7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9fa464001f0316c1be8cd5d26c7830a9047c0817c3f250d9343b3507bb77b4386b7294d8510d6968d719e76524181b62473acbd1b7984ecb13450fe8d055dbf3
|
7
|
+
data.tar.gz: b7c014de9ed8069e698b649fefd966f7e23131542bb0e853909c4cc7b548e62001a4966c0b580926fbd345cdc9ee058c307759572d9deaf4c8c7fbf8089dd9d1
|
data/.DS_Store
ADDED
Binary file
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Ivan Novosad
|
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,41 @@
|
|
1
|
+
# Sniffets
|
2
|
+
|
3
|
+
![Sniffets](http://images4-d.ravelrycache.com/uploads/Tazzyb/195662530/image_small.jpg)
|
4
|
+
|
5
|
+
Simple gem that allows you to inject snippets in pages.
|
6
|
+
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'sniffets'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install sniffets
|
23
|
+
|
24
|
+
Run the generator:
|
25
|
+
|
26
|
+
$ rails generate sniffets:install
|
27
|
+
|
28
|
+
And migrate:
|
29
|
+
|
30
|
+
$ rake db:migrate
|
31
|
+
|
32
|
+
If you need to add authentication for manipulating the snippets, monkeypatch
|
33
|
+
`Admin::BaseController` and add a `before_action`.
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
1. Fork it ( https://github.com/[my-github-username]/sniffets/fork )
|
38
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
39
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
40
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
41
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
class Admin::SnippetsController < Admin::BaseController
|
2
|
+
before_action :set_snippet, only: [:show, :edit, :update, :destroy]
|
3
|
+
|
4
|
+
# GET /snippets
|
5
|
+
# GET /snippets.json
|
6
|
+
def index
|
7
|
+
@snippets = Snippet.all
|
8
|
+
end
|
9
|
+
|
10
|
+
# GET /snippets/1
|
11
|
+
# GET /snippets/1.json
|
12
|
+
def show
|
13
|
+
end
|
14
|
+
|
15
|
+
# GET /snippets/new
|
16
|
+
def new
|
17
|
+
@snippet = Snippet.new
|
18
|
+
end
|
19
|
+
|
20
|
+
# GET /snippets/1/edit
|
21
|
+
def edit
|
22
|
+
end
|
23
|
+
|
24
|
+
# POST /snippets
|
25
|
+
# POST /snippets.json
|
26
|
+
def create
|
27
|
+
@snippet = Snippet.new(snippet_params)
|
28
|
+
|
29
|
+
respond_to do |format|
|
30
|
+
if @snippet.save
|
31
|
+
format.html { redirect_to [:admin, @snippet], notice: 'Snippet was successfully created.' }
|
32
|
+
format.json { render action: 'show', status: :created, location: @snippet }
|
33
|
+
else
|
34
|
+
format.html { render action: 'new' }
|
35
|
+
format.json { render json: @snippet.errors, status: :unprocessable_entity }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# PATCH/PUT /snippets/1
|
41
|
+
# PATCH/PUT /snippets/1.json
|
42
|
+
def update
|
43
|
+
respond_to do |format|
|
44
|
+
if @snippet.update(snippet_params)
|
45
|
+
format.html { redirect_to [:admin, @snippet], notice: 'Snippet was successfully updated.' }
|
46
|
+
format.json { head :no_content }
|
47
|
+
else
|
48
|
+
format.html { render action: 'edit' }
|
49
|
+
format.json { render json: @snippet.errors, status: :unprocessable_entity }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# DELETE /snippets/1
|
55
|
+
# DELETE /snippets/1.json
|
56
|
+
def destroy
|
57
|
+
@snippet.destroy
|
58
|
+
respond_to do |format|
|
59
|
+
format.html { redirect_to admin_snippets_url }
|
60
|
+
format.json { head :no_content }
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
# Use callbacks to share common setup or constraints between actions.
|
67
|
+
def set_snippet
|
68
|
+
@snippet = Snippet.find(params[:id])
|
69
|
+
end
|
70
|
+
|
71
|
+
# Never trust parameters from the scary internet, only allow the white list through.
|
72
|
+
def snippet_params
|
73
|
+
params.require(:snippet).permit(:name, :snippet, :status)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module SnippetsHelper
|
2
|
+
|
3
|
+
def snippet_for(name, default = nil, &block)
|
4
|
+
Rails.cache.fetch("snippet::#{name}") do
|
5
|
+
snippet = Snippet.published.find_by_name(name.to_s)
|
6
|
+
return snippet.to_s.html_safe unless snippet.blank?
|
7
|
+
default = capture(&block) if block_given?
|
8
|
+
return "Snippet for #{name} missing" unless default
|
9
|
+
Snippet.create(name: name, snippet: default) unless Snippet.find_by_name(name.to_s)
|
10
|
+
default.to_s.html_safe
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
alias_method :s, :snippet_for
|
15
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class Snippet < ActiveRecord::Base
|
2
|
+
after_save :clear_cache
|
3
|
+
validates :name, presence: true
|
4
|
+
scope :published, -> { where(status: 'published') }
|
5
|
+
|
6
|
+
def name=(name)
|
7
|
+
write_attribute :name, name.to_s.squish.downcase.tr(" ","_")
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_s
|
11
|
+
snippet
|
12
|
+
end
|
13
|
+
|
14
|
+
def cache_key
|
15
|
+
"snippet::#{name}"
|
16
|
+
end
|
17
|
+
|
18
|
+
def clear_cache
|
19
|
+
Rails.cache.delete(self)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
<%= form_for([:admin, @snippet]) do |f| %>
|
2
|
+
<% if @snippet.errors.any? %>
|
3
|
+
<div id="error_explanation">
|
4
|
+
<h2><%= pluralize(@snippet.errors.count, "error") %> prohibited this snippet from being saved:</h2>
|
5
|
+
|
6
|
+
<ul>
|
7
|
+
<% @snippet.errors.full_messages.each do |msg| %>
|
8
|
+
<li><%= msg %></li>
|
9
|
+
<% end %>
|
10
|
+
</ul>
|
11
|
+
</div>
|
12
|
+
<% end %>
|
13
|
+
|
14
|
+
<div class="form-group">
|
15
|
+
<%= f.label :name %>
|
16
|
+
<%= f.text_field :name, class: "form-control" %>
|
17
|
+
</div>
|
18
|
+
|
19
|
+
<div class="form-group">
|
20
|
+
<%= f.label :snippet %>
|
21
|
+
<%= f.text_area :snippet, class: "form-control", rows: "10" %>
|
22
|
+
</div>
|
23
|
+
|
24
|
+
<div class="form-group">
|
25
|
+
<%= f.label :status %>
|
26
|
+
<%= f.select :status, ['published', 'unpublished'], class: "form-control" %>
|
27
|
+
</div>
|
28
|
+
|
29
|
+
<div class="actions form-actions">
|
30
|
+
<%= f.submit class: "btn btn-primary" %>
|
31
|
+
</div>
|
32
|
+
<% end %>
|
@@ -0,0 +1,34 @@
|
|
1
|
+
<div>
|
2
|
+
<h1>Snippets</h1>
|
3
|
+
<p>
|
4
|
+
<%= link_to 'New Snippet', new_admin_snippet_path, class: "btn btn-primary" %>
|
5
|
+
</p>
|
6
|
+
</div>
|
7
|
+
|
8
|
+
<% if @snippets.present? %>
|
9
|
+
<table class="table">
|
10
|
+
<thead>
|
11
|
+
<tr>
|
12
|
+
<th>Name</th>
|
13
|
+
<th>Snippet</th>
|
14
|
+
<th>Status</th>
|
15
|
+
<th></th>
|
16
|
+
</tr>
|
17
|
+
</thead>
|
18
|
+
|
19
|
+
<tbody>
|
20
|
+
<% @snippets.each do |snippet| %>
|
21
|
+
<tr>
|
22
|
+
<td><%= link_to snippet.name, [:admin, snippet] %></td>
|
23
|
+
<td><%= truncate snippet.snippet %></td>
|
24
|
+
<td><%= snippet.status %></td>
|
25
|
+
<td><%= link_to 'Edit', edit_admin_snippet_path(snippet) %></td>
|
26
|
+
</tr>
|
27
|
+
<% end %>
|
28
|
+
</tbody>
|
29
|
+
</table>
|
30
|
+
<% else %>
|
31
|
+
<p>
|
32
|
+
No snippets
|
33
|
+
</p>
|
34
|
+
<% end %>
|
@@ -0,0 +1,22 @@
|
|
1
|
+
<h1>Snippet</h1>
|
2
|
+
<hr>
|
3
|
+
<p>
|
4
|
+
<%= link_to 'Edit', edit_admin_snippet_path(@snippet), class: "btn btn-primary" %>
|
5
|
+
<%= link_to 'Destroy', [:admin, @snippet], method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-danger" %>
|
6
|
+
</p>
|
7
|
+
<hr>
|
8
|
+
|
9
|
+
<p>
|
10
|
+
<strong>Name:</strong>
|
11
|
+
<%= @snippet.name %>
|
12
|
+
</p>
|
13
|
+
|
14
|
+
<p>
|
15
|
+
<strong>Snippet:</strong>
|
16
|
+
<%= @snippet.snippet %>
|
17
|
+
</p>
|
18
|
+
|
19
|
+
<p>
|
20
|
+
<strong>Status:</strong>
|
21
|
+
<%= @snippet.status %>
|
22
|
+
</p>
|
@@ -0,0 +1 @@
|
|
1
|
+
json.extract! @snippet, :name, :snippet, :status
|
data/lib/.DS_Store
ADDED
Binary file
|
Binary file
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
|
4
|
+
module Sniffets
|
5
|
+
module Generators
|
6
|
+
class InstallGenerator < Rails::Generators::Base
|
7
|
+
include Rails::Generators::Migration
|
8
|
+
|
9
|
+
desc "Generates the necessary migrations and routes"
|
10
|
+
|
11
|
+
source_root File.expand_path("../templates", __FILE__)
|
12
|
+
|
13
|
+
def setup_routes
|
14
|
+
route "\n namespace :admin do\n resources :snippets\n end"
|
15
|
+
end
|
16
|
+
|
17
|
+
def create_migrations
|
18
|
+
migration_template 'migrations/create_snippets.rb', 'db/migrate/create_snippets.rb'
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def self.next_migration_number(path)
|
24
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class CreateSnippets < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :snippets do |t|
|
4
|
+
t.string "name"
|
5
|
+
t.text "snippet"
|
6
|
+
t.string "status", default: "published"
|
7
|
+
t.datetime "created_at"
|
8
|
+
t.datetime "updated_at"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.down
|
13
|
+
drop_table :snippets
|
14
|
+
end
|
15
|
+
end
|
data/lib/sniffets.rb
ADDED
data/sniffets.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sniffets/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sniffets"
|
8
|
+
spec.version = Sniffets::VERSION
|
9
|
+
spec.authors = ["Ivan Novosad, Henrik Sjökvist"]
|
10
|
+
spec.email = ["ivan.novosad@gmail.com, henrik@kollegorna.se"]
|
11
|
+
spec.summary = %q{Snippets}
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_runtime_dependency 'rails', '~> 4.1.5', '~> 4.1.5'
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sniffets
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ivan Novosad, Henrik Sjökvist
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.1.5
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.1.5
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- ivan.novosad@gmail.com, henrik@kollegorna.se
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".DS_Store"
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- app/controllers/admin/base_controller.rb
|
69
|
+
- app/controllers/admin/snippets_controller.rb
|
70
|
+
- app/helpers/snippets_helper.rb
|
71
|
+
- app/models/snippet.rb
|
72
|
+
- app/views/admin/snippets/_form.html.erb
|
73
|
+
- app/views/admin/snippets/edit.html.erb
|
74
|
+
- app/views/admin/snippets/index.html.erb
|
75
|
+
- app/views/admin/snippets/index.json.jbuilder
|
76
|
+
- app/views/admin/snippets/new.html.erb
|
77
|
+
- app/views/admin/snippets/show.html.erb
|
78
|
+
- app/views/admin/snippets/show.json.jbuilder
|
79
|
+
- lib/.DS_Store
|
80
|
+
- lib/generators/.DS_Store
|
81
|
+
- lib/generators/sniffets/install/install_generator.rb
|
82
|
+
- lib/generators/sniffets/install/templates/migrations/create_snippets.rb
|
83
|
+
- lib/sniffets.rb
|
84
|
+
- lib/sniffets/engine.rb
|
85
|
+
- lib/sniffets/version.rb
|
86
|
+
- sniffets.gemspec
|
87
|
+
homepage: ''
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.4.5
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Snippets
|
111
|
+
test_files: []
|