Skip to content

Commit d53062e

Browse files
committed
Initial commit
0 parents  commit d53062e

10 files changed

+203
-0
lines changed

.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
tmp
18+
.idea

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in rails_admin_toggleable.gemspec
4+
gemspec

LICENSE.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2013 Gleb Tv
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.

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# RailsAdminToggleable
2+
3+
Make any boolean field easily toggleable on\off from index view in rails admin
4+
5+
## Installation
6+
7+
Add this line to your application's Gemfile:
8+
9+
gem 'rails_admin_toggleable'
10+
11+
And then execute:
12+
13+
$ bundle
14+
15+
Or install it yourself as:
16+
17+
$ gem install rails_admin_toggleable
18+
19+
## Usage
20+
21+
Add the toggleable action:
22+
23+
RailsAdmin.config do |config|
24+
config.actions do
25+
......
26+
toggle
27+
end
28+
end
29+
30+
Make the field you need toggleable:
31+
32+
rails_admin do
33+
list do
34+
field :enabled, :toggle
35+
...
36+
end
37+
...
38+
end
39+
40+
## Contributing
41+
42+
1. Fork it
43+
2. Create your feature branch (`git checkout -b my-new-feature`)
44+
3. Commit your changes (`git commit -am 'Add some feature'`)
45+
4. Push to the branch (`git push origin my-new-feature`)
46+
5. Create new Pull Request

Rakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require "bundler/gem_tasks"

lib/rails_admin_toggleable.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require "rails_admin_toggleable/version"
2+
3+
module RailsAdminToggleable
4+
end
5+
6+
require 'rails_admin/config/actions'
7+
require 'rails_admin/config/model'
8+
9+
require 'rails_admin_toggleable/action'
10+
require 'rails_admin_toggleable/field'

lib/rails_admin_toggleable/action.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module RailsAdmin
2+
module Config
3+
module Actions
4+
class Toggle < Base
5+
RailsAdmin::Config::Actions.register(self)
6+
7+
# Is the action acting on the root level (Example: /admin/contact)
8+
register_instance_option :root? do
9+
false
10+
end
11+
12+
register_instance_option :collection? do
13+
false
14+
end
15+
16+
# Is the action on an object scope (Example: /admin/team/1/edit)
17+
register_instance_option :member? do
18+
true
19+
end
20+
21+
register_instance_option :controller do
22+
Proc.new do |klass|
23+
p params
24+
if params['id'].present?
25+
obj = @abstract_model.model.find(params['id'])
26+
method = params[:method]
27+
obj.send(method + '=', params[:on] == '1' ? true : false)
28+
end
29+
end
30+
end
31+
32+
register_instance_option :link_icon do
33+
'icon-move'
34+
end
35+
36+
register_instance_option :http_methods do
37+
[:post]
38+
end
39+
end
40+
end
41+
end
42+
end

lib/rails_admin_toggleable/field.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require 'builder'
2+
3+
module RailsAdmin
4+
module Config
5+
module Fields
6+
module Types
7+
class Toggle < RailsAdmin::Config::Fields::Base
8+
# Register field type for the type loader
9+
RailsAdmin::Config::Fields::Types::register(self)
10+
11+
register_instance_option :view_helper do
12+
:check_box
13+
end
14+
15+
register_instance_option :formatted_value do
16+
case value
17+
when nil
18+
%{<span class="badge">-</span>}
19+
when false
20+
link_to '&#x2718;'.html_safe, toggle_path(model_name: @abstract_model, method: name, on: '1'), class: 'badge badge-important'
21+
when true
22+
link_to '&#x2713;'.html_safe, toggle_path(model_name: @abstract_model, method: name, on: '0'), class: 'badge-success'
23+
end.html_safe
24+
end
25+
26+
register_instance_option :export_value do
27+
value.inspect
28+
end
29+
30+
# Accessor for field's help text displayed below input field.
31+
register_instance_option :help do
32+
""
33+
end
34+
end
35+
end
36+
end
37+
end
38+
end

lib/rails_admin_toggleable/version.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module RailsAdminToggleable
2+
VERSION = "0.0.1"
3+
end

rails_admin_toggleable.gemspec

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- encoding: utf-8 -*-
2+
lib = File.expand_path('../lib', __FILE__)
3+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4+
require 'rails_admin_toggleable/version'
5+
6+
Gem::Specification.new do |gem|
7+
gem.name = "rails_admin_toggleable"
8+
gem.version = RailsAdminToggleable::VERSION
9+
gem.authors = ["Gleb Tv"]
10+
gem.email = ["[email protected]"]
11+
gem.description = %q{TODO: Write a gem description}
12+
gem.summary = %q{TODO: Write a gem summary}
13+
gem.homepage = ""
14+
15+
gem.files = `git ls-files`.split($/)
16+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18+
gem.require_paths = ["lib"]
19+
end

0 commit comments

Comments
 (0)