Grails Domain Class String List - list

i try to make a String list in Grails.
My part therefore are:
class Name{
static hasMany = [names: String] }
I created also a Controller for the Class and added there:
static scaffold = Name
So far it is working but on in the view there are only the name of the String but i can not add any inputs to the list.
So therefore I'm looking for a solution, hope someone can help.
Thanks in advance!

You question is not very clear because of that what you want.
If i have understood you question here are my answers to you :
1.If you use scaffolding true grails will automatically generate pages and codes based on your domain configuration, if you need more inputs as you said add more domain variables like.i.e
class Name{
static hasMany = [names: String]
//to see more inputs add them here
String description
}
You have only one domain called Name and then what did you expect to see other more inputs you need other more member variables or property's..
If you are not satisfied with the component that grails created fro you can change it on .gsp file.
Great resource here http://goo.gl/f7XV8I
Let me know of further assistance...

Related

Get Ember component's name

If I call Ember.inspect(component), I get a response like:
<app#component:my-component::ember1246>
This suggests that the component is aware of its own name (my-component). Is there a way to access just that name?
Ember.inspect() calls the objects toString() which in turn calls some internal ember metal functions to derive the name.
There is however an internal property which people have been using to get the name:
this.__proto__._debugContainerKey
This outputs component:my-component, then you can just split on :. There's an issue which will create a public way of exposing object meta information which we'll be able to use in the future: https://github.com/emberjs/ember.js/issues/10742
This regex will do the trick:
//Returns a string with full name of component, like: <ProjectName#component:path/to/component-name::ember793>
let componentName = this.toString ();
//This regex will match the component and capture the latest part of component path.
let regex = componentName.match (/<[a-zA-Z]+#[a-zA-Z]+:(?:[a-z]+[\/])*([-a-z]+)::[a-zA-Z]+[0-9]+>/);
//The latest element of array is the component name.
console.log (regex[regex.length-1]); //component-name
See https://regex101.com/r/rX9bQ7/3 for full explanation about how this works.

Yii dynamic model id

So I'm working on some unit tests and relational fixtures.
I'm creating a model dynamically like:
$model = CActiveRecord::model('Post');
$post = $model->findByPk(1);
But after that I cannot for some reason get $post->id. I traced the problem to CActiveRecord class:
public function __get($name)
{
if(isset($this->_attributes[$name]))
return $this->_attributes[$name];
...
Where $name = "id". It says that $this->_attributes[$name] does not exist! As a matter of fact _attributes is empty.
My Post class does not define id (or any other properties) as a public property and I don't want to do so either. I just let the AR map it to table columns for me.
What am I missing?
Edit 1
My fixtures are regular Yii fixtures - nothing really special about them.
What differs is the way I load them really. I extended the CDbFixtureManager to be able to specify the order in which they should be loaded by overloading load() method. Only thing of interest that actually fails is that in the fixtures that have foreign keys I use the following:
'comment1' => array('post_id' => $this->getRecord('Post', 'post1')->id);
That's where it fails. getRecord returns the actual Post record (since I know the Post fixture has already been successfully loaded and exists in DB), but on the ->id part I get an exception about that attribute not existing.
If I go into Post model and add public $id; to it, then everything works! But I'm not sure if it's good practice to go about declaring all properties public like that.
If you look at this page carefully:
http://www.yiiframework.com/doc/guide/1.1/en/test.unit
you'll see that they use an array form for retrieving fixtures:
$this->posts['sample1']['id']
There is an alias defined in their fixture array for each record and fixture items aren't loaded as models really ...
Does that help? If not, it would be helpful to see your fixture file :-)
I think I found the root cause of this issue for me. While my FixtureManager was using the testdb DBConnection, the models still used the regular one.
For whatever reason, my debugger was giving me misleading errors like the one described in my original post.
Once I was able to set the DBConnection of all Models in the unit test the puzzle snapped into place and everything is now working smoothly!

Mocking dynamic finder in grails unit tests

I am using grails 1.3.7.
While writing the unit tests for my controller, I am getting errors as my action in the controller uses dynamic finder for example "findByName('ABC')".
I understand that I need to mock the dynamic finders as well and I tried that thing out.But unfortunately didnt work out.
I request you to please help me out with the exact mocking snippet I need to use.
Also, for all thses kinds of mocking things, please suggest some documentaion URLs which will have all these.
Also, In my grails app, I have associataion between three domains as "hasMany and belongsTo". So while writing unit tests for controllers, please suggest how exactly I need to mock these domain classes.
Please I request all of you to answer asap as I am stuck with these doubts.
Thanks in Advance,
and awaiting for positive answers.
Thanks
If name is a property of the domain, then the dynamic finders will "automatically" be mocked when you mock the domain. You should simply be able to do:
mockDomain(YourDomain)
At this point you will just have null returned, so create some instances if you want that query to return them:
YourDomain instance = new YourDomain(name: 'foo')
mockDomain(YourDomain, [instance])
then you can do:
assert instance == YourDomain.findByName('foo')
Anyone got an update for this question with Grails 4.0.0 and Spock? Because it doesn't seem to have working dynamic finders.
To mock a domain dynamic finder try:
def someDomainMock = new GrailsMock(SomeDomain)
someDomainMock.demand.static.findByName(1..1) { String name ->
[new SomeDomain(name: name + '_1'), new SomeDomain(name: name + '_2')]
}

Scala template accessing model object properties in Play 2.0

I have a simple question regarding accessing member variables of a model object.
I have the following model objects:
#Entity
public class Person extends Model{
#Id
public Long id;
public String name;
}
#Entity
public class Account extends Model{
#Id
public String email;
public String password;
#OneToOne
public Person person;
}
So far so good, Any given person can have a single account. The Account object is copied from the zentask example. After authentication I redirect to the index page which displays the user realname as stated in the Person.name member variable. The Account object is inserted in the page just as with the zentasks example like so:
Account.find.byId(Controller.request().username());
Now the following strange things happen in the template which i do not understand:
#account.person.name
results in a Null value inserted in the template while calling:
#account.person.getName() or #account.person.getName
results as expected with the correct name inserted from the person object.
#account.person
shows the .toString() of the person object, also correctly showing the name.
So to summarize: What is wrong with the code above? Why can I call the account.person value without any problems, but when I call account.person.name this does not work anymore
Thank you in advance!
Richard
This is because JPA uses Aspects to intercept getter usage and fill-in the missing data from objects that are lazy-loaded. I don't know what conventional thinking is, but I would not use public members ever with JPA for this reason, it will break the framework consistently.
If you really want to use public members, you'll have to mark relationships as eager fetching:
#OneToMany(fetch=FetchType.EAGER)
or explicitly fetch all of the object tree you'll need in your template (ugh).
In your case, the relationship, a OneToOne is defined on the other side of the relationship, if you define it on the Account side, it should fetch eager by default. I forget if you can define OneToOne on both entities, I think you can, but you might have to fiddle with it a bit.
Overall, don't use public members with JPA, it will break. Better yet, ditch JPA and use Anorm instead, it maps to the problem domain much more successfully than JPA. Issues like this consistently cause JPA implementations to take up twice as much implementation time as anyone seems able to predict.
I just stumbled upon an answer posted by Guillaume Bort, which explains things.
Read here:
https://groups.google.com/d/topic/play-framework/CNjH3w_yF6E/discussion
Hope this helps!
Because of lazy loading the values in the field only get loaded when you access them from the class itself.(something that, in normal circumstances would use a setter/getter
In order to load the values you ether have to write getters and setters.
Or you can create a methode that checks every value.
you can add the following methode to your Account Entity:
public void checker(){
if(email==null){}
if(password==null){}
if(person==null){}
}
this will load every value, but won't reduce performance

Service design: where should I put my validation code?

I'm a little confused about the concept of services.
Let's assume i want to create a new user.
Right now, i'm checking if all fields are non-empty in Model (project.Web solution) and in UserServices too (project.Services solution). But to validate the email address i have to create a new function.
Should i create that class in project.Services, something like GeneralValidation.cs and use it, or should i separate it from the Services and create a new project?
Until now i didn't create two solutions for Repositories and Services. I just had one solution for testing, other for project.Web, and another solution where i had the a domain folder, repository folder, and a few classes for business logic, but after reading a little about design patterns i've decided to split this.
Maybe i haven't understand yet the meaning of Services. For me, a service is a layer that will consume a repository (like user), and the service layer is where i should do the validation. If this is correct, that's why i don't know where to create the function for email validation for example.
If someone could explain me this i would really be appreciated. I've already read blogs articles, and search similar questions in stackoverflow but i can't be sure if i've really understand it.
Thanks
Services and Repositories are 'first class citizens' in Domain Driven Design, so I do not see why you'd want to put them in a separate project ?
For your specific scenario, why don't you create a 'UserService' which has a method 'CreateUser', which looks for instance like this:
public static class UserService
{
public static CreateUser( User u )
{
var userRepository = RepositoryFactory.CreateUserRepository();
userRepository.Save (u);
SendActivationMailForUser(u);
}
private static void SendActivationMailForUser( User user )
{
....
}
}
Some more information about services can be found here