Skip to content

Commit 7350207

Browse files
authored
Merge pull request #9 from workgena/rails-5-1-compability
Rails 5 1 compability
2 parents 6d2354f + 8d3d840 commit 7350207

File tree

10 files changed

+258
-33
lines changed

10 files changed

+258
-33
lines changed

Gemfile

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
source "http://rubygems.org"
2-
# Specify your gem's dependencies in active_admin_sidebar.gemspec
1+
source 'https://rubygems.org'
32

3+
# Specify your gem's dependencies in activeadmin_scoped_collection_actions.gemspec
44
gemspec
5+
6+
group :test do
7+
gem 'sprockets-rails', '3.2.0'
8+
gem 'rails', '5.1.1'
9+
gem 'turbolinks'
10+
gem 'rspec-rails'
11+
gem 'activeadmin', '1.0.0'
12+
gem 'sass-rails'
13+
gem 'sqlite3'
14+
gem 'launchy'
15+
gem 'database_cleaner'
16+
gem 'capybara'
17+
gem 'selenium-webdriver'
18+
gem 'poltergeist'
19+
end

README.md

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,81 @@
1-
# ActiveAdminSidebar
1+
# ActiveAdmin Sidebar
22

3-
Provides ability to manipulate sidebar position with activeadmin (tested with activeadmin ~> 1.0.0.pre)
3+
Provides ability to manipulate sidebar position for ActiveAdmin (tested with ActiveAdmin ~> 1.0.0)
44

5+
## Install
56

6-
Add including of css file
7+
```ruby
8+
gem 'active_admin_sidebar'
9+
# or latest from GitHub
10+
gem 'active_admin_sidebar', git: 'https://github.com/activeadmin-plugins/active_admin_sidebar.git'
11+
```
12+
13+
Add including of CSS file
714

815
```scss
9-
@import "active_admin_sidebar";
16+
@import "active_admin_sidebar";
1017
```
1118

1219
to the
13-
```
14-
app/assets/stylesheets/active_admin.css.scss
20+
```
21+
app/assets/stylesheets/active_admin.css.scss
1522
```
1623

1724
And including of coffee file (optional, need only for collapsed sidebar)
1825

19-
```js
20-
//= require active_admin_sidebar
26+
```coffeescript
27+
#= require active_admin_sidebar
2128
```
2229

2330
to the
24-
```
31+
32+
```scss
2533
app/assets/javascripts/active_admin.js
2634
```
2735

28-
Changing sidebar position dynamically with before_filter
36+
# Configuration per resource
37+
38+
Changing sidebar position dynamically with before_action
39+
2940
```ruby
3041
# app/admin/posts.rb
3142
ActiveAdmin.register Post do
32-
before_filter :left_sidebar!, only: [:show]
43+
before_action :left_sidebar!, only: [:show]
3344
end
3445

3546
# app/admin/comments.rb
3647
ActiveAdmin.register Comment do
37-
before_filter :right_sidebar!
48+
before_action :right_sidebar!
3849
end
3950
```
4051

52+
## Global configuration
4153

42-
Moving sidebar to the left within all resource (config/initializers/active_admin.rb)
54+
Moving sidebar to the left within all resource. Set configuration in `config/initializers/active_admin.rb`
4355

4456
```ruby
45-
# == Controller Filters
57+
# == Controller before-actions
4658
#
47-
# You can add before, after and around filters to all of your
48-
# Active Admin resources from here.
49-
#
50-
config.before_filter do
51-
left_sidebar! if respond_to?(:left_sidebar!)
52-
end
59+
# You can add before, after and around actions to all of your resources
60+
ActiveAdmin.setup do |config|
61+
config.before_action do
62+
left_sidebar! if respond_to?(:left_sidebar!)
63+
end
64+
end
5365
```
5466

55-
Also you can use sidebar collapsing. It will add css icon in title of first sidebar will save current state in session
67+
## Collapsing sidebar
68+
69+
You can use sidebar collapsing.
70+
It will add "hide/show" button. Shown/Hidden state is persisted across all pages.
71+
5672
```ruby
5773
left_sidebar!(collapsed: true)
5874
```
59-
You can override button color according to your color theme:
60-
```css
75+
76+
You can override button color according to your color theme. For example:
77+
78+
```scss
6179
body.active_admin {
6280
#active_admin_content.left_sidebar, #active_admin_content.collapsed_sidebar {
6381
.collapse_btn, .uncollapse_btn {
@@ -70,8 +88,3 @@ You can override button color according to your color theme:
7088
Example
7189

7290
![Alt text](https://raw.githubusercontent.com/activeadmin-plugins/active_admin_sidebar/master/screen/sidebar.jpg "Example")
73-
74-
75-
76-
77-

Rakefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
require "bundler/gem_tasks"
1+
require "bundler"
2+
require 'rake'
3+
Bundler.setup
4+
Bundler::GemHelper.install_tasks
5+
6+
# Import all our rake tasks
7+
FileList['tasks/**/*.rake'].each { |task| import task }

lib/active_admin_sidebar/positions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def right_sidebar!
1818
def collapsed_sidebar
1919
if request.xhr?
2020
if params[:collapsed_sidebar].present?
21-
collapsed = ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include? params[:collapsed_sidebar]
21+
collapsed = params[:collapsed_sidebar].to_s == 'true'
2222
session[:collapsed_sidebar] = collapsed
2323
render json: { collapsed_sidebar: collapsed } and return
2424
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module ActiveAdminSidebar
2-
VERSION = "0.1.0.rc3"
2+
VERSION = "1.0.0"
33
end

spec/sidebars_spec.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
require 'spec_helper'
2+
3+
describe 'authors index', type: :feature, js: true do
4+
5+
before do
6+
Author.create!(name: 'John', last_name: 'Doe')
7+
Author.create!(name: 'Jane', last_name: 'Roe')
8+
add_author_resource
9+
add_post_resource
10+
end
11+
12+
context 'left-sidebar with default settings' do
13+
before do
14+
visit '/admin/authors'
15+
end
16+
17+
it 'has left-sidebar with colapse-button' do
18+
expect(page).to have_css('#filters_sidebar_section')
19+
expect(page).to have_css('#filters_sidebar_section .collapse_btn.icono-caret-left')
20+
expect(page).to have_css('#active_admin_content.with_sidebar.left_sidebar.collapsible_sidebar')
21+
end
22+
23+
context 'when click on Collapse' do
24+
before do
25+
page.find('#filters_sidebar_section .collapse_btn').click
26+
end
27+
28+
it "sidebar is hidden, and save it's state after going to another page" do
29+
expect(page).to have_css('#sidebar', visible: :hidden)
30+
31+
# Posts page is configured as: "before_action :skip_sidebar!"
32+
visit '/admin/posts'
33+
# sidebar does not exists at all
34+
expect(page).to have_css('#page_title', text: 'Posts')
35+
expect(page).not_to have_css('#sidebar', visible: :all)
36+
37+
visit '/admin/authors'
38+
# sidebar is hidden
39+
expect(page).to have_css('#page_title', text: 'Authors')
40+
expect(page).to have_css('#sidebar', visible: :hidden)
41+
42+
page.find('.uncollapse_btn').click
43+
44+
# sidebar is visible
45+
expect(page).to have_css('#sidebar', visible: :visible)
46+
expect(page).to have_css('.collapse_btn')
47+
expect(page).not_to have_css('.uncollapse_btn')
48+
end
49+
end
50+
51+
end
52+
53+
end

spec/spec_helper.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
$LOAD_PATH.unshift(File.dirname(__FILE__))
2+
$LOAD_PATH << File.expand_path('../support', __FILE__)
3+
4+
ENV['BUNDLE_GEMFILE'] = File.expand_path('../../Gemfile', __FILE__)
5+
require "bundler"
6+
Bundler.setup
7+
8+
ENV['RAILS_ENV'] = 'test'
9+
# Ensure the Active Admin load path is happy
10+
require 'rails'
11+
ENV['RAILS'] = Rails.version
12+
ENV['RAILS_ROOT'] = File.expand_path("../rails/rails-#{ENV['RAILS']}", __FILE__)
13+
# Create the test app if it doesn't exists
14+
unless File.exists?(ENV['RAILS_ROOT'])
15+
system 'rake setup'
16+
end
17+
18+
require 'active_model'
19+
# require ActiveRecord to ensure that Ransack loads correctly
20+
require 'active_record'
21+
require 'active_admin'
22+
ActiveAdmin.application.load_paths = [ENV['RAILS_ROOT'] + "/app/admin"]
23+
require ENV['RAILS_ROOT'] + '/config/environment.rb'
24+
# Disabling authentication in specs so that we don't have to worry about
25+
# it allover the place
26+
ActiveAdmin.application.authentication_method = false
27+
ActiveAdmin.application.current_user_method = false
28+
29+
require 'rspec/rails'
30+
require 'support/admin'
31+
require 'capybara/rails'
32+
require 'capybara/rspec'
33+
require 'capybara/poltergeist'
34+
35+
36+
RSpec.configure do |config|
37+
config.use_transactional_fixtures = false
38+
39+
config.before(:suite) do
40+
DatabaseCleaner.strategy = :truncation
41+
DatabaseCleaner.clean_with(:truncation)
42+
end
43+
config.before(:each) do
44+
DatabaseCleaner.strategy = :truncation
45+
DatabaseCleaner.start
46+
end
47+
config.after(:each) do
48+
DatabaseCleaner.clean
49+
end
50+
51+
end
52+
53+
# RSpec.configure do |config|
54+
# config.before(:each, js: true) do
55+
# page.driver.browser.manage.window.maximize if page.driver.browser.respond_to?(:manage)
56+
# end
57+
# end
58+
# Capybara.javascript_driver = :selenium
59+
60+
Capybara.register_driver :poltergeist do |app|
61+
Capybara::Poltergeist::Driver.new(app, {
62+
js_errors: true,
63+
timeout: 80,
64+
debug: true,
65+
:phantomjs_options => ['--debug=no', '--load-images=no']
66+
})
67+
end
68+
Capybara.javascript_driver = :poltergeist

spec/support/admin.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def add_author_resource(options = {}, &block)
2+
3+
ActiveAdmin.register Author do
4+
config.filters = true
5+
end
6+
7+
Rails.application.reload_routes!
8+
9+
end
10+
11+
12+
def add_post_resource(options = {}, &block)
13+
14+
ActiveAdmin.register Post do
15+
config.filters = true
16+
before_action :skip_sidebar!
17+
end
18+
19+
Rails.application.reload_routes!
20+
21+
end

spec/support/rails_template.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Rails template to build the sample app for specs
2+
3+
generate :model, 'author name:string{10}:uniq last_name:string birthday:date'
4+
generate :model, 'post title:string:uniq body:text author:references'
5+
6+
#Add validation
7+
inject_into_file "app/models/author.rb", " validates_presence_of :name\n validates_uniqueness_of :last_name\n", after: "Base\n"
8+
inject_into_file "app/models/post.rb", " validates_presence_of :author\n", after: ":author\n"
9+
10+
# Configure default_url_options in test environment
11+
inject_into_file "config/environments/test.rb", " config.action_mailer.default_url_options = { :host => 'example.com' }\n", after: "config.cache_classes = true\n"
12+
13+
# Add our local Active Admin to the load path
14+
inject_into_file "config/environment.rb",
15+
"\n$LOAD_PATH.unshift('#{File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'lib'))}')\nrequire \"active_admin\"\n",
16+
after: "require File.expand_path('../application', __FILE__)"
17+
18+
run "rm Gemfile"
19+
20+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
21+
22+
generate :'active_admin:install --skip-users'
23+
generate :'formtastic:install'
24+
25+
# Initialize plugin
26+
inject_into_file "config/initializers/active_admin.rb",
27+
" config.before_action do\n left_sidebar!(collapsed: true) if respond_to?(:left_sidebar!)\n end\n\n",
28+
after: "ActiveAdmin.setup do |config|\n"
29+
30+
inject_into_file "app/assets/stylesheets/active_admin.scss",
31+
"@import \"active_admin_sidebar\";\n",
32+
after: "@import \"active_admin/base\";\n"
33+
34+
inject_into_file "app/assets/javascripts/active_admin.js.coffee",
35+
"#= require active_admin_sidebar\n",
36+
after: "#= require active_admin/base\n"
37+
38+
run "rm -r test"
39+
run "rm -r spec"
40+
41+
route "root :to => 'admin/dashboard#index'"
42+
43+
rake "db:migrate"

tasks/test.rake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
desc "Creates a test rails app for the specs to run against"
2+
task :setup do
3+
require 'rails/version'
4+
system("mkdir spec/rails") unless File.exists?("spec/rails")
5+
system "bundle exec rails new spec/rails/rails-#{Rails::VERSION::STRING} -m spec/support/rails_template.rb --skip-spring"
6+
end

0 commit comments

Comments
 (0)