redmine settings tab project - ruby

I want to put a new tab in the setting project on a Redmine plugin and I don't find how to do it.
I get the code of CCUL Project Managment plugin by ISITROL S.A.(www.isitrol.com) and replace de funcionlity with my funcionality, but I still can't change the name of the config_controller to my controller.
the helperpatch has this estructure:
require 'redmine'
require_dependency 'projects_helper'
module CCULProjectsHelperPatch
def self.included(base)
base.send(:include, ProjectsHelperMethodsCCUL)
base.class_eval do
unloadable
alias_method_chain :project_settings_tabs, :coste
end
end
end
module ProjectsHelperMethodsCCUL
def project_settings_tabs_with_coste
#tabs = project_settings_tabs_without_coste
#action = {:name => 'coste', :controller => 'coste_config', :action => :index, :partial => 'coste_config/index', :label => :tab_ccul}
Rails.logger.info "old_tabs: #{#tabs}"
Rails.logger.info "action: #{#action}"
#tabs << #action #if User.current.allowed_to?(action, #project)
#tabs
end
end
ProjectsHelper.send(:include, CCULProjectsHelperPatch) unless ProjectsHelper.included_modules.include? CCULProjectsHelperPatch
I've changed the name of the controller(and controller file's name), the view's folder and the name of the controller in the init file (I change pm_config to coste_pm)
This is the init file:
Rails.configuration.to_prepare do
require_dependency 'coste_project_helper_patch'
end
Redmine::Plugin.register :redmine_coste do
name 'Costes'
author 'Me'
description 'Plugin para calculo coste proyecto. Compatible con Redmine 2.3.x '
version '0.1.4'
project_module :coste do
permission :cost_manager, {:coste_config => [:index, :create], :coste_project => [:show]}
end
menu :project_menu, :coste, { :controller => 'coste_project', :action => 'show' }, :caption => :coste, :param => :project_id
menu :admin_menu, :coste, { :controller => 'coste_admin', :action => 'show' }, :caption => :coste
end
The error is "404 The page you were trying to acess doesn't exist or has been removed" when I try to access Settings of one project
I don't know what I need to change
I hope you can help me

I've found the problem, in the views templates of the settings the form had the old controller's name.
For who is looking for a way to put the setting project tab like me: I need take the database information in the view. Redmine doesn't call the controller when it creates the project settings tabs
And the code above is the basic to do it

Related

How to cache index page with pagination?

Suppose this my one of admin model
ActiveAdmin.register Theme do
menu :if => proc{ current_admin_user.super_admin?}
after_filter :only => [:create, :update, :destroy] do
expire_action :action => :show
expire_action :action => :index
end
controller do
caches_action :index, :show
end
end
with this code caching is done, but on index page with pagination i am facing issue, that is i am not able to visit another page of Theme.
How to implement something like this
https://github.com/amatsuda/kaminari#creating-friendly-urls-and-caching
this is what fixed the issue:
caches_action :index, :cache_path => Proc.new { |c| c.params }
This will generate new cache for specific paginate param's
Read More

Paperclip "Gem" Rails 3.1 undefined method model file name

Using rails 3.1.1, Ruby 1.9.2,
Gems: gem 'haml', gem 'simple_form', gem 'aws-sdk',
gem 'paperclip', :git => "git://github.com/thoughtbot/paperclip.git"
plugin: country_select: git://github.com/rails/country_select.git
Having an issue uploading/displaying images pushed to Amazon S3 through paperclip (GEM)
Error: undefined method `avatar_file_name' for #Player:0x00000102aff228
For the most part I was following the example on the git-hub page for paperclip
https://github.com/thoughtbot/paperclip
Here is what I have in my code:
Migration: 20111224044508_create_players.rb
class CreatePlayers < ActiveRecord::Migration
def change
create_table :players do |t|
t.string :first_name
t.boolean :first_name_public, :default => false
...
t.string :website
t.boolean :website_public, :default => false
t.has_attached_file :avatar
t.timestamps
end
end
end
Model: Player.rb:
class Player < ActiveRecord::Base
attr_accessible :first_name, ... :website
validates_presence_of :username, :email
has_attached_file :avatar,
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => ":class/:id/:style/:filename"
{Unrelated validations}
end
S3 file: s3.yml
development:
bucket: voh_development
access_key_id: *********************
secret_access_key: ********************
staging:
bucket: voh_staging
access_key_id: *********************
secret_access_key: ********************
production:
bucket: voh_production
access_key_id: *********************
secret_access_key: ********************
Controller: players_controller.rb
class PlayersController < ApplicationController
def create
#player = Player.create(params[:player])
if #player.save
redirect_to players_path, :notice => "Player Created";
else
render :action => 'new'
end
end
{basic restful}
end
Views:
Edit.html.haml + New.html.haml
= simple_form_for #player do |f|
= f.input :first_name
...
= f.input :website
= f..file_field :avatar
.input_div
= f.button :submit
index.html.haml
...
%td Avatar
%td First Name
...
%td Actions
- #players.each do |player|
%tr
%td
= image_tag #player.avatar.url(:thumb)
%td
= player.first_name
...
%td
= link_to ' Show ', player_path(player.id)
|
= link_to ' Edit ', edit_player_path(player.id)
show.html.haml
= image_tag #user.avatar.url
%br
= #player.first_name
...
Research:
I found a lot to do with the pluging and genration of the migration but it all seems old. Most of them suggest putting in the up down in the migration for the 4 attributes. However it seems that should have been replaced by the one line t.has_attached_file :avatar.
I have a rails 3.0 project and this worked. I am able to upload products and pull them back down. (had to play with the suggested image_tag #icon.avatar.url and turned it into %img{:src => URI.unescape(icon.icon.url)} but that a different question.)
TLDR: Fixed typo in index.html.haml from #player => player, Added :avatar to attr_accessible.
I woke up this morning and had a different error.
instead of: undefined method Avatar_file_name'
I got: undefined method avatar' for nil:NilClass
That error was caused buy a simple type in my code. I used an instance vairable instead of .each variable I should have been using (index.html.haml:9)
Now the app was not erring out but the file was still not uploading.
In the development log I found this. (I did not look here the first time I posted)
WARNING: Can't mass-assign protected attributes: avatar
I then went and added :Avatar to attr_accessible and everything started working.
Not sure if this is supposed to be required or not but I did see that they had updated S3 header to be a proc yesterday.
https://github.com/thoughtbot/paperclip/tree/master/test/storage
I am not going to close this out yet. There will be an edit because I am going to play with the version and quickly report my findings today. Then I will close this out.
Edit:
I tryed switch back to 2.4.5 and I am getting the error that made me switch to pulling master in the first place. When attempting to do a migration with t.has_attached_file :avatar it fails to migrate and gives the following error.
undefined method `has_attached_file' for #ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::TableDefinition:0x00000105053600
I think I will stick with pulling from master.

error to customize error 404 rails 3.1 with hight_voltage gem

Hi guys I remove /page from high_voltage gem with this answer.
Remove page/ of High Voltage for statics page rails
I have in my routes for high_voltage this:
match '/:id' => 'high_voltage/pages#show', :as => :static, :via => :get
For maintenance page 404 in rails 3.1 I follow this fix http://techoctave.com/c7/posts/36-rails-3-0-rescue-from-routing-error-solution with errors_controller.rb with the next code:
def routing
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
Then I add to routes.rb the next code for maintenance page 404 in rails
match '*a', :to => 'errors#routing'
The problem is that if I put in browser www.mydomain.com/sdfs dont working 404 system error and show No such page: sdfs
but however if I put www.mydomain.com/a_controller/action/sdfs yes working fine the fix for error 404 page.
I think that problem is my routes.rb
I solved this problem by extending the HighVoltage::PagesController and modifying the error catching:
class PagesController < HighVoltage::PagesController
rescue_from ActionView::MissingTemplate do |exception|
render_not_found
end
end
In my case though, my 404 function resides in my application controller so that it can easily be called from any location. If you make the same change, you will also need to update your route:
match '/:id' => 'pages#show', :as => :static, :via => :get
Thank you kevinthopson for mi this dont working fine :(.
I have in my routes.rb:
match '/:id' => 'pages#show', :as => :static, :via => :get
I have added this in pages_controller.rb
class PagesController < HighVoltage::PagesController
rescue_from ActionView::MissingTemplate do |exception|
render_not_found
end
end
I have added this code to aplication_controller.rb:
def render_not_found
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
Working fine if you put now:
localhost:3000/dfadsfadsf
The problem now is that if you put for example that routes in navigation bar:
localhost:3000/users_or_static_page/asdfadfadfa
Dont working for me :(.

Rails 3 - Restricting formats for action in resource routes

I have a resource defined in my routes.
resources :categories
And I have the following in my Category controller:
def show
#category = Category.find(params[:id])
respond_to do |format|
format.json { render :json => #category }
format.xml { render :xml => #category }
end
end
The controller action works fine for json and xml. However I do NOT want the controller to respond to html format requests. How can I only allow json and xml? This should only happen in the show action.
What is the best way to achieve this?
Also is there any good tips for DRYing up the respond_to block?
Thanks for your help.
I found that this seemed to work (thanks to #Pan for pointing me in the right direction):
resources :categories, :except => [:show]
resources :categories, :only => [:show], :defaults => { :format => 'json' }
The above seems to force the router into serving a format-less request, to the show action, as json by default.
You must wrap those routes in a scope if you want to restrict them to a specific format (e.g. html or json). Constraints unfortunately don't work as expected in this case.
This is an example of such a block...
scope :format => true, :constraints => { :format => 'json' } do
get '/bar' => "bar#index_with_json"
end
More information can be found here: https://github.com/rails/rails/issues/5548
This answer is copied from my previous answer here..
Rails Routes - Limiting the available formats for a resource
You could do the following in your routes.rb file to make sure that only the show action is constrained to json or xml:
resources :categories, :except => [:show]
resources :categories, :only => [:show], :constraints => {:format => /(json|xml)/}
If this doesn't work you could try explicitly matching the action:
resources :categories, :except => [:show]
match 'categories/:id.:format' => 'categories#show', :constraints => {:format => /(json|xml)/}
constraints was not working for POST requests and then I tried defaults it works for all.
namespace :api, :defaults => { :format => 'json' } do
namespace :v1 do
resources :users do
collection do
get 'profile'
end
end
post 'signup' => 'users#create'
post 'login' => 'user_sessions#create'
end
end
I was using Rails 4.2.7

ActionMailer 3 without Rails

I'm writing a small Ruby program that will pull records from a database and send an HTML email daily. I'm attempting to use ActionMailer 3.0.3 for this, but I'm running in to issues. All the searching I've done so far on using ActionMailer outside of Rails applies to versions prior to version 3. Could someone point me in the right direction of where to find resources on how to do this? Here's where I am so far on my mailer file:
# lib/bug_mailer.rb
require 'action_mailer'
ActionMailer::Base.delivery_method = :file
class BugMailer < ActionMailer::Base
def daily_email
mail(
:to => "example#mail.com",
:from => "example#mail.com",
:subject => "testing mail"
)
end
end
BugMailer.daily_email.deliver
I'm definitely stuck on where to put my views. Every attempt I've made to tell ActionMailer where my templates are has failed.
I guess I should also ask if there's a different way to go about accomplishing this program. Basically, I'm doing everything from scratch at this point. Obviously what makes Rails awesome is it's convention, so is trying to use parts of Rails on their own a waste of time? Is there a way to get the Rails-like environment without creating a full-blown Rails app?
After some serious debugging, I found how to configure it.
file mailer.rb
require 'action_mailer'
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "domain.com.ar",
:authentication => :plain,
:user_name => "test#domain.com.ar",
:password => "passw0rd",
:enable_starttls_auto => true
}
ActionMailer::Base.view_paths= File.dirname(__FILE__)
class Mailer < ActionMailer::Base
def daily_email
#var = "var"
mail( :to => "myemail#gmail.com",
:from => "test#domain.com.ar",
:subject => "testing mail") do |format|
format.text
format.html
end
end
end
email = Mailer.daily_email
puts email
email.deliver
file mailer/daily_email.html.erb
<p>this is an html email</p>
<p> and this is a variable <%= #var %> </p>
file mailer/daily_email.text.erb
this is a text email
and this is a variable <%= #var %>
Nice question! It helped me to understand a bit more how Rails 3 works :)
It took me a while to get this to work in (non-)Rails 4. I suspect it's just because I have ':require => false' all over my Gemfile, but I needed to add the following to make it work:
require 'action_view/record_identifier'
require 'action_view/helpers'
require 'action_mailer'
Without the above code, I kept getting a NoMethodError with undefined method 'assign_controller'.
After that, I configured ActionMailer as follows:
ActionMailer::Base.smtp_settings = {
address: 'localhost', port: '25', authentication: :plain
}
ActionMailer::Base.default from: 'noreply#example.com'
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.logger = Logger.new(STDOUT)
ActionMailer::Base.logger.level = Logger::DEBUG
ActionMailer::Base.view_paths = [
File.join(File.expand_path("../../", __FILE__), 'views', 'mailers')
# Note that this is an Array
]
The templates go in lib/<GEM_NAME>/views/mailers/<MAILER_CLASS_NAME>/<MAILER_ACTION_NAME>.erb (MAILER_ACTION_NAME is the public instance method of your mailer class that you call to send the email).
Lastly, don't forget to put this in your spec_helper:
ActionMailer::Base.delivery_method = :test

Resources