how to edit menu bar in active admin? - change the language. Rails - ruby-on-rails-4

I'm using Active Admin with i18n. Everything works but if i want to change language I need to do that by hand - changing site address :-(... How to add buttons in navigate bar with models names? It could be another place but good for that - accessible.

You can set a default locale in a before filter by adding the following lines to application_controller.rb:
before_filter :set_locale
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
def default_url_options(options={})
{ :locale => I18n.locale }
end
Add a menu for selecting languages in active_admin.rb:
config.namespace :admin do |admin|
admin.build_menu :utility_navigation do |menu|
menu.add :label => "Languages" do |lang|
lang.add :label => "English",:url => proc { url_for(:locale => 'en') }, id: 'i18n-en', :priority => 1
lang.add :label => "Mexican",:url => proc { url_for(:locale => 'es') }, id: 'i18n-es', :priority => 2
end
menu.add :label => proc { display_name current_active_admin_user },
:url => '#',
:id => 'current_user',
:if => proc { current_active_admin_user? }
admin.add_logout_button_to_menu menu
end
end
You can set the default locale in application.rb:
config.before_configuration do
I18n.load_path += Dir[Rails.root.join('config', 'locales', '*.{rb,yml}').to_s]
I18n.default_locale = :en
I18n.reload!
end

Related

How to display current_admin_user's name in navigation header?

Construction like:
menu.add :label => current_admin_user.full_name do |submenu|
menus.each do |m|
submenu.add :label => m['name'],
:url => "/admin/change/#{m['id']}",
:html_options => {:style => 'float:left;'},
:if => proc { current_admin_user.role?('super_admin') }
end
end
Doesn't work in 1.0pre+ anymore. There is no current_admin_user in the scope. But in the logs i see the request that fetches admin user before a menu getting added.
SELECT `admin_users`.* FROM `admin_users` WHERE `admin_users`.`id` = 6 ORDER BY `admin_users`.`id` ASC LIMIT 1
Any chance to deal with it?
It is still possible to use, i only needed to pass id of a menu
menu.add id: 'user_name' label: -> { current_admin_user.full_name } do |submenu|

"data has contents that are not what they are reported to be" paperclip with ckeditor

I am using paperclip with ckeditor and I get this error when i click on "send it to the server" : "data has contents that are not what they are reported to be".
That's what "attachment_file.rb" and "picture.rb" look like:
class Ckeditor::AttachmentFile < Ckeditor::Asset
has_attached_file :data, :styles => { :content => '575>', :thumb => '80x80#' },
:storage => :s3, :s3_credentials => "/config/s3.yml", :path => ":attachment/:id/:style.:extension",
:url => ":s3_domain_url"
validates_attachment_presence :data
validates_attachment_size :data, less_than: 100.megabytes
do_not_validate_attachment_file_type :data
def url_thumb
#url_thumb ||= Ckeditor::Utils.filethumb(filename)
end
end
you should remove the do_not_validate_attachment_file_type :data line from your code, or just change the editor .

Delayed_Paperclip + Sidekiq + Mongoid-Paperclip

I'm working on an Rails 4.1 engine that handles user uploads of photos and videos. I'm using Mongoid-Paperclip to handle the uploads and Paperclip-av-transcoder to encode the videos into several formats. All files are stored at S3. All of that works fine, but as you can expect, encoding the videos can take quite some time, so the next step is to make that happen in the background. I did some googling and found Delayed_Paperclip that seems to do what I need. After that it seemed that Sidekiq was the best option for handling the background processing.
Now the problem is, I can't make all this work together. Running my unit tests I get NoMethodError: undefined method 'process_in_background' so it seems the problem resides on Delayed_Paperclip, although there is no special setup to it.
This is the model firing the problem
module MyEngine
class Video
include Mongoid::Document
include Mongoid::Paperclip
has_mongoid_attached_file :file,
:path => ':hash.:extension',
:hash_secret => "the-secret",
:storage => :s3,
:url => ':s3_domain_url',
:s3_credentials => File.join(Rails.root, 'config', 's3.yml'),
:bucket => "my-bucket-#{Rails.env}",
:styles => {
:mp4 => { :format => 'mp4', :convert_options => { :output => { :vcodec => 'libx264', :acodec => 'copy' } } },
:ogg => { :format => 'ogg', :auto_rotate => true },
:webm => { :format => 'webm', :auto_rotate => true },
:thumb => { :geometry => "250x187#", :format => 'jpg', :time => 10, :auto_rotate => true }
},
:processors => [:transcoder]
validates_attachment :file, :content_type => { :content_type => ["video/x-flv", "video/mp4", "video/ogg", "video/webm", "video/x-ms-wmv", "video/x-msvideo", "video/quicktime", "video/3gpp"] }
process_in_background :file
end
end
I've tried adding require "delayed_paperclip" to the lib/myengine/myengine.rb file but that didn't help.
Regarding Sidekiq, I have added to the test_helper.rb the following:
require 'sidekiq/testing'
Sidekiq::Testing.inline!
Note that I did not forget to run bundle install and Redis is up and running. I'm using Mongoid, not active record.
What I am doing wrong? Has anyone successfully used this setup? Is there another combination of gems that I should try?
Aditional info:
Delayed_paperclip 2.9.1
Mongoid 4.0.2
Mongoid-Paperclip 0.0.9
Paperclip 4.2.1
Paperclip-av-transcoder 0.6.4
Rails 4.1.9
Sidekiq 3.5.0
I've been digging through the code of delayed_paperclip and it is definitely tied to ActiveRecord so not compatible with Mongoid. I gave a try to mongoid_paperclip_queue but that gem hasn't been updated in 4 years and doesn't seem to work with the current versions of rails/mongoid/paperclip as far as I can tell.
I therefore decided the best way to solve my issue would be to override the code of delayed_paperclip that integrates with ActiveRecord and make it instead work with Mongoid.
This is what I ended up doing, and seems to be working fine so far:
lib/myengine.rb
require "mongoid_paperclip"
require "paperclip/av/transcoder"
require "delayed_paperclip"
require "myengine/engine"
module Myengine
end
DelayedPaperclip::Railtie.class_eval do
initializer 'delayed_paperclip.insert_into_mongoid' do |app|
ActiveSupport.on_load :mongoid do
DelayedPaperclip::Railtie.insert
end
if app.config.respond_to?(:delayed_paperclip_defaults)
DelayedPaperclip.options.merge!(app.config.delayed_paperclip_defaults)
end
end
# Attachment and URL Generator extends Paperclip
def self.insert
Paperclip::Attachment.send(:include, DelayedPaperclip::Attachment)
Paperclip::UrlGenerator.send(:include, DelayedPaperclip::UrlGenerator)
end
end
DelayedPaperclip::InstanceMethods.class_eval do
def enqueue_post_processing_for name
DelayedPaperclip.enqueue(self.class.name, read_attribute(:id).to_s, name.to_sym)
end
end
Then all you need is to include the delayed_paperclip glue to the model:
module Myengine
class Video
include Mongoid::Document
include Mongoid::Paperclip
include DelayedPaperclip::Glue # <---- Include this
has_mongoid_attached_file :file,
:path => ':hash.:extension',
:hash_secret => "the-secret",
:storage => :s3,
:url => ':s3_domain_url',
:s3_credentials => File.join(Rails.root, 'config', 's3.yml'),
:bucket => "my-bucket-#{Rails.env}",
:styles => {
:mp4 => { :format => 'mp4', :convert_options => { :output => { :vcodec => 'libx264', :acodec => 'copy' } } },
:ogg => { :format => 'ogg', :auto_rotate => true },
:webm => { :format => 'webm', :auto_rotate => true },
:thumb => { :geometry => "250x187#", :format => 'jpg', :time => 10, :auto_rotate => true }
},
:processors => [:transcoder]
validates_attachment :file, :content_type => { :content_type => ["video/x-flv", "video/mp4", "video/ogg", "video/webm", "video/x-ms-wmv", "video/x-msvideo", "video/quicktime", "video/3gpp"] }
process_in_background :file
end
end
Hopefully this will save somebody else all the trouble.

undefined method `registrationsController' for Devise:Module

I am getting the following error with navigating to my sign in/signup pages:
undefined method `registrationsController' for Devise:Module
/users/registrations_controller.rb
class Users::RegistrationsController < Devise::registrationsController
# def new
# super
# end
# def create
# super
# end
end
/config/routes.rb
Rails.application.routes.draw do
get 'spotkeys/spot_page'
root 'spotkeys#index'
get 'index' => 'spotkeys#index'
get 'dashboard' => 'spotkeys#dashboard'
post 'dashboard' => 'spotkeys#dashboard'
get 'settings' => 'spotkeys#settings'
get 'key_settings' => 'spotkeys#key_settings'
get 'qr_codes/new'
get 'qr_codes/create'
get 'settings' => 'spotkeys#settings'
get 'key_settings' => 'spotkeys#key_settings'
match 'dashboard' => 'spotkeys#dashboard', as: :new_spotkey, via: [:get, :post]
get 'signup', to: 'users/registrations#new', :as => :new_user_session
post 'signin', to: 'users/sessions#create', :as => :user_session
delete 'signout', to: 'users/sessions#destroy'
resources :qr_codes, only: [:new, :create]
resources :spotkeys
resources :keys
devise_for :users,
:controllers => { :registrations => 'users/registrations',
:confirmations => 'users/confirmations',
:sessions => 'users/sessions',
:omniauth_callbacks => 'omniauth_callbacks'
},
:to => 'users/sessions#destroy', :as => :destroy_user_session,
:skip => [:sessions] do
end
end
I am using:
rails 4.2.0
ruby 2.1.5p273
Your inheritance is defined wrong. You mistyped the lowercase r instead of the uppercase R in the parent class. You should define your controller like this:
class Users::RegistrationsController < Devise::RegistrationsController

bootstrap typehead dropdown doesn't expand over bootstrap collapse

So I have bootstrap collapse and inside it I have tabs, and inside one of the tabs I have a form with text_field that has bootstrap typeahead and the problem is that typeahead's dropdown dosn't expand over collapse.
that text_field with autocomplete is the last element in there.
here is the picture.
I want that dropdown expands below the collapse element (below the line on the picture)
EDIT:
Here is the haml for that view
- #i = 0
- #trainings.each do |training|
- #i = #i+1
.accordion#accordion2
.accordion-group
.accordion-heading
%a{:class => 'accordion-toggle', 'data-toggle' => 'collapse', :href => "#collapse#{#i}"}
= "Training #{#i}"
%div{:id => "collapse#{#i}", :class => 'accordion-body collapse'}
.accordion-inner
%pre= "Description: #{training.description}"
%ul.nav.nav-tabs#myTab
%li.active
%a{"data-toggle" => "tab", :href => "#planirano#{#i}"} Planirano
%li
%a{"data-toggle" => "tab", :href => "#napravljeno#{#i}"} Napravljeno
.tab-content
%div{:class => 'tab-pane active', :id => "planirano#{#i}"}
- training.exercises.each do |exercise|
%pre= "#{exercise.element.name} | #{exercise.description} | #{exercise.number_of_series}"
= form_for :training_exercise, :url => training_exercises_path(:training => training.id), remote: true, html: {:class => 'form-inline'} do |f|
= f.label :exercise
= f.text_field :exercise, :id => 'add_training_exercise'
= f.button :Add, :class => 'btn'
%div{:class => 'tab-pane', :id => "napravljeno#{#i}"} to sam napravio
f.text_ifeld :exercise, :id => 'add_training_exercise' is the field with autocomplete I am asking about.
EDIT:
and here is the rendered HTML
I somehow find the answer on stack overflow the solution is
.accordion-body.in { overflow:visible; }
It is from here.
I am sorry for asking question that already has the answer but I really was not able to find it because I didn't guess the right word for searching.
Applying the following css works only partially, accordion-body.in { overflow:visible; }, since it only displays the overflow of the "slice" that is being expanded. You'd need to apply it to the parent as well. In addition the above css affects the expand/collapse effect; i.e. the content of what is being shown get's displayed over the accordion, versus gradually being shown. A solution I tried is to:
1. Apply the overflow:visible only to the parent, i.e. #myAccordion { overflow:visible } AND
2. Apply overflow:visible only to the "slice" being opened when it is needed (on open), and removing it on close, like so:
$("#myAccordion").collapse();
// Here, we are attaching event handlers to the accordion "slice" body so that we can update it's overflow when opened AND when it's about to be closed.
$("#myAccordion .accordion-body").on("shown", function () {
$(this).css("overflow", "visible");
});
$("#myAccordion .accordion-body").on("hide", function () {
$(this).css("overflow", "hidden");
});
This worked for me since my typeahead is in a navbar
.navbar-collapse.in{
/*allows typeahead to overflow nav bar during collapse*/
overflow-y:initial !important;
}