问题
I checked IRC for redmine and was unable to get help. I was conflicted on weather to stick this on Superuser, ServerFault or here, but as my problem is technically programming oriented I decided to look for help here.
We have a Mercurial repository system with a layout based on projects that addresses our needs. I wrote some shell scripts which work delightfully to manage the repository and put them in correct places etc. I am trying to call these scripts and pass them parameters from Redmine. I am editing the app/controllers/projects_controller.rb (lines 75 -> 87)
I have managed to pull the project parameters and current user, but I have two custom fields I added (using the custom fields in Redmine Administration) and I am trying to access the values of these custom fields. Does anyone have any idea how I can get these?
My current working test statement is below:
system "echo '#{@project.identifier}, #{User.current}' >> /tmp/rm.log"
回答1:
Use the CustomField model. For example,
# Find the first Custom Field
cf = CustomField.first
# Get the name
puts cf.name
# Find out if this Custom Field is for all projects
cf.is_for_all?
# If not, find out which projects are using it
cf.projects
To figure this out, I just installed Redmine-1.0.0 and poked around in the source and the script/console.
回答2:
use DBI;
$dbServer='';
$user='';
$pass='';
$ident=$ARGV[0];
my $dsn = "dbi:mysql:database=redmine;host=$dbServer;port=3306";
my $dbh = DBI->connect($dsn, "$user","$pass") or die "Can't connet to the Database: $DBI::errstr\n";
my $sth = $dbh->prepare("SELECT value FROM custom_values c INNER JOIN projects p ON c.customized_id=p.id WHERE p.identifier='$ident' LIMIT 1");
$sth -> execute();
my @row=$sth->fetchrow_array();
my $serverParams=@row[0];
Calling the above script (instead of echoing in the original), and then passing in the project identifier which we already have, the custom parameter can be used in whatever way nessacary. This code it to grab a single custom field (i was only using one.)
来源:https://stackoverflow.com/questions/3215902/redmine-ruby-looking-for-the-value-of-a-custom-field