Let's integrate with the features module. Also see #350881: Make exporting to module easier for related discussion.

Comments

fago’s picture

Component: Rules Core » Rules Engine
Status: Active » Needs work

Here is an initial patch - not ready yet.

Problem:
Rules is exporting both rules + rules sets. Features seems to be built for selecting only initial component. We could go and just show "Rule: Label" and "Rule Set: Label" in the list, but two separate sections would be definitely nicer. As first start, the patch only deals with rules.

So far:
* Module dependencies + revert is missing.
* Exports only rules, no sets.

As alternative we could also rely on the rule categories and only allow to export by category - as rules + rule sets implementing a feature should be ideally in the same category nevertheless. Opinions?

amitaibu’s picture

You forgot the patch :)

fago’s picture

StatusFileSize
new4.98 KB

ouch, thanks. Here it is.

fago’s picture

amitaibu’s picture

Cool, I see some of the code from the 'export to module' issue is used :)

amitaibu’s picture

> As alternative we could also rely on the rule categories and only allow to export by category - as rules + rule sets implementing a feature should be ideally in the same category nevertheless.

I think that's a good practice but I don't think we should force users to do it. Ideally we should have 3 sections like in the current rules export for rules/ rules-sets/category

btw, I have noticed that the rules name aren't renamed.

fago’s picture

StatusFileSize
new8.59 KB

Great, it's in!! :)

So I did some further work on this patch. This work depends on the patch in #524160: Allow one default hook to contain multiple components.

Status:
* Added feature components: rules, rule sets, rule items by category
* Fixed rule-renaming to be actually used.
* Improved rule-renaming to not collide with existing rule-names of an already existing feature. (If you add new rules during feature-update.)

Though there is still an issue with rule renaming - features doesn't cope with that currently. One possibility would be to rename the rule in rules_features_export(). So I wonder if it's ok to put the rule item in features $data keyed by the new name.

Also I wonder whether I should mark the exported tags as actually exported components? I tend to think it would be better to not do so, as working "overridden" detection won't work right for them.

TODO:
* Make sure that features default/overridden detection works right.
* Implement revert
* Rename rule-sets to fit the feature's namespace.
* Add dependencies on required modules

However I think for now we could also go without the last point.

fago’s picture

Also TODO:
* Only show triggered rules in the "rules part". So best also rename to that component to "Triggered rules".

amitaibu’s picture

> Add dependencies on required modules

#524834: Add module dependency information to item

fago’s picture

Thanks to some further input from yhahn I think we shouldn't rename rule-sets but allow the user to change the prefix, which currently is enforced to be "rules_" for custom-sets. Thus all possible references to the rule-set will be alive.

For triggered rules itself I think renaming is best - as there is no manual name yet and referencing triggered rules somewhere doesn't look like making much sense to me.

dagmar’s picture

Hi.

I'm triying to modify this patch but I have a problem with the cvs versions.

This is what i'm doing.

cvs -z6 -d:pserver:anonymous:anonymous@cvs.drupal.org:/cvs/drupal-contrib checkout -d rules -r DRUPAL-6--1 contributions/modules/rules/

modifications....

cvs diff -up > ~/Desktop/rules_features_3.patch

But i'm getting more diferences that I made. For example:

-// $Id: rules.export.inc,v 1.1.2.1 2009/07/15 16:00:00 fago Exp $
+// $Id$
 
 /**
  * @file Provides export functionality and integrates with the features module.
  */
 
 /**
- * Export all items given to code, where $export has to be an array of arrays
+ * Export all items given, where $export has to be an array of arrays

I didn't change these lines ¬¬

What I'm doing wrong?

dagmar’s picture

StatusFileSize
new9.68 KB

Ok, I discovered the problem. part of #350881: Make exporting to module easier was commited and for this reason the patch is a bit diferent.

Well, this patch add a way to get the module dependencies for rules.

Maybe there is a better way to do this, but this works.

It seems that there isn't a way to get the module for the "event" for the rules, but actions and conditions are covered.

fago’s picture

oh, the 'module' property contains the readable module name which might differ - so it's better if we just check which module provides the _info. Then we can also cover events.

The features views integration does it already that way, see views_plugin_dependencies() (in the features module). It should work the same way for rules.

yhahn’s picture

Just a quick heads up that in features HEAD hook_features_api() has been refactored -- the new format is cleaner and easier to understand. It should return an array of keyed component information, so that each component can define its own options separately:

function rules_features_api() {
  return array(
    'rules' => array(
      'feature_source' => TRUE,
      'default_hook' => 'rules_defaults',
      'file' => drupal_get_path('module', 'rules') .'/rules.export.inc',
    ),
    'rules_sets' => array(
      ...
    ),
  );
}

We'll try to get a stable-ish tag of features HEAD out soon.

klausi’s picture

Issue tags: +gsoc:rulesmonkey
StatusFileSize
new11.46 KB

New patch:
* added module dependency retrieval
* switched to new features_api()

TODO:
* rename rules to avoid collisions
* Implement revert

klausi’s picture

Status: Needs work » Needs review
StatusFileSize
new11.4 KB

Another patch:
* added revert()
* removed rules within rule sets from the rules export selection

I removed renaming of rules for now, because I haven't found a satisfying solution yet. rules_features_export() may be called twice, so prefixing rule names there is also done twice. But we need to prefix them there, because that information is written to the .info file. Then we also need to rename the rules during the actual export, where the $data parameter comes in renamed already and must be named back to the original name to find the rule. Afterwards the rule names must be renamed again.

All in all very ugly .... ideas welcome, maybe I missed something.

dagmar’s picture

StatusFileSize
new20.08 KB

I have made some testing for this patch:

It seems to work fine, however there is a interesting thing to fix.

If diff module is installed, features allows users to see the differences between an unchanged rule and a overwritten rule.

For this example I only have changed the value of the condition "User has role" to 'Negated' and diff shows a lot of changes.

Well, in fact the only change that matters is

-	'#negate' => 0,	+	'#negate' => 1,

But instead of that I'm getting something else (the code attached in this issue).

Maybe the solution for this is include all the values for a rule when it is exported, for example, even if users didn't check in an options, export it with its default value

What do you think?

fago’s picture

@dagmar: If this are all changes the right side looks broken too me as the removed lines are necessary.

@rule renaming: hm we need to find a solution for that, as we can't go without it. Else users would end up with rule-name clashes and might overwrite the default rules with newly added ones - ouch.

fago’s picture

I thought a bit about the renaming problem and I think best would be something like a hook_features_export_alter() - where every module can inspect the export and apply any final changes. Thus we could rename the rules there and change $data to use the new rule names as keys and the old ones as values.

dagmar’s picture

@fago: The removed lines are added again at the bottom of the file. I think that rules is rebuilding the export in a different way.

amitaibu’s picture

Just a remark #419644: Use CTools to export . Features module already supports CTools export, so maybe our effort should be towards integration with CTools? (for Rules 2 of course).

klausi’s picture

Status: Needs review » Needs work

@fago: Posted a hook request in the features issue queue: #551490: Allow renaming of components

criz’s picture

subscribe...

Desperately waiting for the features integration feature!

fago’s picture

Status: Needs work » Fixed

I tackled this one again and finally I got it working right! :)

-> It's depending on the patch from #551490: Allow renaming of components

I took the work from klausi and improved it further. So now when an exported rule invokes a rule-set, this is detected and it's added in now too! Also the exported rules and sets get tagged by the feature name automatically now. However detection of the used input evaluators is missing.

amitaibu’s picture

Very cool! we'll check it out soon :)

fago’s picture

I just refactored it a bit:

>refactored features integration to allow actions to add further components to the export by implementing rules_action_callback_features_export().

-> So e.g. the cck integration could add in required fields.

fago’s picture

>However detection of the used input evaluators is missing.

I just added that too, so it should be basically complete now :)

fago’s picture

Status: Fixed » Needs work

Hm, setting back to needs work, as it's still open how to solve the problem in features. The current code works fine with the patch from http://drupal.org/node/551490#comment-1966640 however we probably should improve it to only store a rule name mapping in $data in the first place.

fago’s picture

Status: Needs work » Fixed

Yeppie, it's in -> It's already usable now, so I close this issue. Let's handle the remaining stuff in #573586: Fix features integration.

JacobSingh’s picture

Awesome! I'm a very excited beta tester :)

I'll try it out next week.

Status: Fixed » Closed (fixed)
Issue tags: -gsoc:rulesmonkey

Automatically closed -- issue fixed for 2 weeks with no activity.